尝试在laravel视图中显示时解析错误

时间:2018-09-15 16:23:36

标签: php laravel

每当我试图在视图中显示数据库中的项目时,都会出现此错误。

  

未定义变量:制造商(查看:... / index.blade.php)

在我的控制器中:

public function index(Request $request){
    $title = array('pageTitle' => Lang::get("website.Home"));
    $result = array();  

    $manufacturers = DB::table('manufacturers')
    ->leftJoin('manufacturers_info','manufacturers_info.manufacturers_id', '=', 'manufacturers.manufacturers_id')
    ->select('manufacturers.manufacturers_id as id', 'manufacturers.manufacturers_image as image',  'manufacturers.manufacturers_name as name', 'manufacturers_info.manufacturers_url as url', 'manufacturers_info.url_clicked', 'manufacturers_info.date_last_click as clik_date')
    ->get();
    $result['manufacturers'] = $manufacturers;
    return view("index", $title)->with('result', $result); }

我的观点:

{{ $manufacturer->name }}   

帮助将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:0)

在控制器中:

$result['manufacturers'] = $manufacturers;
return view('index', [
            $result => $result,
]);

在视图中:

<ul>
   @foreach ($result['manufacturers'] as $manufacturer) 
       <li>{{ $manufacturer->name }} </li>
   @foreach
</ul>

答案 1 :(得分:0)

如果要将制造商数组放入$ result中并传递$ result,则必须在视图中以$ result引用它。

function rest(arr, counter = 0) 
{
  if (counter === arr.length)
{
  return [];
}
  else
    {
      rest(arr, counter + 1)[0] = arr[counter]
    }
}

类似...

   $result['manufacturers'] = $manufacturers;
   return view("index", $title)->with('result', $result);