未定义的变量template_2 laravel 5.2

时间:2017-11-23 08:47:44

标签: php laravel laravel-5.2

使用以下代码获取错误

$result = User::where("device_token", "!=", "")->where("device_type", "!=", "")->get();
        $template_2 = DB::table('notification_templates')->get();
return view('admin.push_notifications')
        ->withPage('push_notifications')
        ->with([
                'result' => $result,
                'template_2' => $template_2,
            ]);

视图:

 @if(($template_2))
    @foreach($template_2 as $taken)

     {{ $taken->title }

       @endforeach
   @endif

PHP v5.5.9

1 个答案:

答案 0 :(得分:0)

根据您的问题,您的代码必须正常工作..如果不起作用,您可以尝试以下代码: -

$result = User::where("device_token", "!=", "")->where("device_type", "!=", "")->get();
$template = DB::table('notification_templates')->get();
return view('admin.push_notifications')->with(compact('result','template'));

查看应该是: -

@if(isset($template))
  @foreach($template as $taken)

 {{ $taken->title }

   @endforeach
@endif

希望它有所帮助!