Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
laravel |如何在JavaScript中访问会话数组?_Javascript_Arrays_Laravel_Laravel 5.4_Laravel Blade - Fatal编程技术网

laravel |如何在JavaScript中访问会话数组?

laravel |如何在JavaScript中访问会话数组?,javascript,arrays,laravel,laravel-5.4,laravel-blade,Javascript,Arrays,Laravel,Laravel 5.4,Laravel Blade,我正在从控制器向视图传递一个数组 以下是控制器功能: $notification = array( 'message' => 'Welcome Admin!', 'alert_type' => 'success', ); return redirect('/home')->with('notification', $notification); <script> @if(Session::has

我正在从控制器向视图传递一个数组

以下是控制器功能:

$notification = array(
            'message' => 'Welcome Admin!', 
            'alert_type' => 'success',
        );
return redirect('/home')->with('notification', $notification);
<script>
  @if(Session::has('notification'))//this line works as expected

    var type = "{{ Session::get('alert_type', 'info') }}";
   //but the type var gets assigned with default value(info)
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;

        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif
</script>
在我看来:

$notification = array(
            'message' => 'Welcome Admin!', 
            'alert_type' => 'success',
        );
return redirect('/home')->with('notification', $notification);
<script>
  @if(Session::has('notification'))//this line works as expected

    var type = "{{ Session::get('alert_type', 'info') }}";
   //but the type var gets assigned with default value(info)
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;

        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif
</script>
但现在我得到一个错误,说

正在尝试获取非对象的属性(视图:C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php)(视图:C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php)


有人能帮我吗?

而不是
{{{Session::get('message')}

分别尝试
{Session::get('notification')->message}
{Session::get('notification')->alert_type}


会话消息返回数组,您需要使用数组键而不是键直接获取消息。

而不是
{{{session::get('message')}}

分别尝试
{Session::get('notification')->message}
{Session::get('notification')->alert_type}


会话消息返回数组,您需要使用数组键而不是直接使用键来获取消息。

您应该将PHP和Javascript代码分开。在HTML中使用
data
属性,并在Javascript代码中获取值

例如,这个HTML代码(我使用
json\u encode
来支持换行符):

您可以通过以下方式缩短JS:

(function(){
    // Don't go any further down the script if [data-notification] is not set.
    if ( ! document.body.dataset.notification)
        return false;

    var type = document.body.dataset.notificationType;
    var types = ['info', 'warning', 'success', 'error'];

    // Check if `type` is in our `types` array, otherwise default to info.
    toastr[types.indexOf(type) !== -1 ? type : 'info'](JSON.parse(document.body.dataset.notificationMessage));

    // toastr['info']('message') is the same as toastr.info('message')
})();


阅读更多关于:,

您应该将PHP和Javascript代码分开。在HTML中使用
data
属性,并在Javascript代码中获取值

例如,这个HTML代码(我使用
json\u encode
来支持换行符):

您可以通过以下方式缩短JS:

(function(){
    // Don't go any further down the script if [data-notification] is not set.
    if ( ! document.body.dataset.notification)
        return false;

    var type = document.body.dataset.notificationType;
    var types = ['info', 'warning', 'success', 'error'];

    // Check if `type` is in our `types` array, otherwise default to info.
    toastr[types.indexOf(type) !== -1 ? type : 'info'](JSON.parse(document.body.dataset.notificationMessage));

    // toastr['info']('message') is the same as toastr.info('message')
})();


阅读更多:,

好的,在从@milo526答案中得到提示后,我做了更多的研究并找到了解决方案@milo526的解决方案尝试以对象的形式访问阵列,因此Laravel嘎嘎作响。我这样做了,现在它工作了

var type = "{{ Session::get('notification')['alert_type'], 'info' }}";
switch(type){
    case 'info':
        toastr.info("{{ Session::get('notification')['message'] }}");
        break;

    case 'warning':
        toastr.warning("{{ Session::get('notification')['message'] }}");
        break;

    case 'success':
        toastr.success("{{ Session::get('notification')['message'] }}");
        break;

    case 'error':
        toastr.error("{{ Session::get('notification')['message'] }}");
        break;
}

好的,在接受@milo526答案的提示后,我做了更多的研究并找到了解决方案@milo526的解决方案尝试以对象的形式访问阵列,因此Laravel嘎嘎作响。我这样做了,现在它工作了

var type = "{{ Session::get('notification')['alert_type'], 'info' }}";
switch(type){
    case 'info':
        toastr.info("{{ Session::get('notification')['message'] }}");
        break;

    case 'warning':
        toastr.warning("{{ Session::get('notification')['message'] }}");
        break;

    case 'success':
        toastr.success("{{ Session::get('notification')['message'] }}");
        break;

    case 'error':
        toastr.error("{{ Session::get('notification')['message'] }}");
        break;
}

好的,但我在尝试获取非对象的属性时遇到了一个错误,这是我所做的
var type=“{{Session::get('notification')->alert_type,'info'}”好的,但我在尝试获取非对象的属性时出错,这是我所做的
var type=“{{Session::get('notification')->alert_type,'info'}”感谢您向我展示这种方式,但您的答案对我无效,因为我正在将数组传递到会话中。所以我做了
{{Session::get('notification')['message']}}
,它是有效的(请参见我的答案),我将尝试按照您的方式进行此小更正谢谢您向我展示了这种方式,但您的答案对我不起作用,因为我正在将数组传递到会话中。因此,我做了
{{Session::get('notification')['message']}}
并且它工作正常(请参见我的答案),我将尝试按照您的方式做一些小的修改