我运行代码时会出现以下错误。怎么了?
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'']);