"Undefined variable: cust (View: C:xampphtdocsONLINE1EXAMresourcesviewsinternalcustom



我正在尝试传递数组customer.blade.php,我知道web.php文件不是这样做的正确位置,但我看到一些视频,数组是这样传递的,web.php文件和customer.blade.php文件

Route::get('customer', function () {
    $cust=['ganshyam','ram','mohan']; 
    return view('internal.customer' ,['cust=>$customer']);
});

客户刀片.php

<h1>Our customers</h1>
<ul>
@foreach($cust as $cust1)
    <li>{{$cust1}}</li>
@endforeach
</ul>

未定义的变量:cust (视图:C:\xampp\htdocs\ONLINE1EXAM\resources\views\internal\customer.blade.php(

更新以下代码行:

return view('internal.customer' ,['cust=>$customer']);

所以它看起来像下面这样。

//Please note that, name of your array is $cust
return view('internal.customer' ,['cust'=>$cust]);

将$cutomer更改为$cust

Route::get('customer', function () { 
    $cust=['ganshyam','ram','mohan']; 
    return view('internal.customer' ,['cust'=>$cust]);
});

如果要传递给视图的变量名称与控制器上的变量名称相同,则可以执行以下操作:

$customers=['ganshyam','ram','mohan']; 
$fruits=['mango','strawberry','grapes']; 
$vegetables=['eggplant','cucumber','carrot']; 
return view('internal.customer', compact('customers', 'fruits', 'vegetables'));

然后在您的秃头文件上:

<h1>Our customers</h1>
<ul>
@foreach($customers as $customer)
   <li>{{$customer}}</li>
@endforeach
</ul>

相关内容

最新更新