Laravel 5.5.13中的视图中未定义的可变误差



我运行代码时会出现以下错误。怎么了?

Undefined variable: tasks (View: C:UsersSilvercoverblogresourcesviewsPageshome.blade.php)

这是我的控制器:

namespace AppHttpControllers;
use IlluminateHttpRequest;
class PagesController extends Controller
{
    public function home() {
        $tasks = ['Jack','Sara'];
        return view("Pages.home")->with(compact($tasks));
    }
}

这是我的观点:

@extends('Layout.layout')
<h3>Names:</h3>
<hr>
@foreach ($tasks as $task)
    {{$task}}
@endforeach

我正在使用Laravel 5.5.13。

执行此操作:

返回视图(" pages.home",compact('tasks'));

with()方法只要求您发送变量的名称,而不是变量本身:

返回视图(" pages.home") -> with('tasks');

如果有多个变量使用数组发送它们:

返回视图(" pages.home",compact(['tasks','othertaiable','enose'']));

返回视图(" pages.home") -> with(['tasks',''othots variable','othere'']);

最新更新