在Laravel 5.6中将多个变量传递给一个视图 [英] passing multiple variable to one view in laravel 5.6

查看:44
本文介绍了在Laravel 5.6中将多个变量传递给一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将多个变量传递给一个视图这是我的 CategoryController.php

hello to all I want to pass multiple variables to one view this is my CategoryController.php

    public function site()
{
    $categories = Category::all();
    return view('template.sitemap', ['categories' => $categories]);
}

这是 SubCategoryController.php

public function index2(){
    $subcategories =  SubCategory::all();
    return view('template.sitemap',['subcategories'=>$subcategories]);
}

这是我在 web.php

Route::get('sitemap.html','CategoryController@site')->name('sitemap')
Route::get('sitemap.html','SubCategoryController@index2')->name('sitemap');

这是我正在尝试执行的视图sitemap.blade.php

and this is the view i am trying to do this sitemap.blade.php

   @foreach($categories as $category)
      <li><a href="category.html">{{$category->name}}</a></li>
      <ul>
       @foreach($subcategories as $subcategory)
         <li><a href="category.html">{{$subcategory->category_name->name}</li>
       @endforeach
      </ul>
  @endforeach

但是我经常看到未定义的变态他们一个人工作很好但是当我希望用户两个变量都看到不确定的可变性时.

but i constantly see undefind vairalble alone they work good but when i want user both variables seee undefined vairable.

推荐答案

您的站点将转到第一个路由,而永远不会转到第二个控制器.您应该写.

Your site will go to the first route and will never go to your second controller. You should rather write.

路线

 Route::get('sitemap.html','CategoryController@site')->name('sitemap');

控制器

  public function site(){
      $data =  array();
      $data['subcategories']  =  SubCategory::all();
      $data['categories']     =  Category::all();
      return view('template.sitemap',compact("data"));
   }

查看

    @foreach($data['categories'] as $category)
    <li><a href="category.html">{{$category->name}}</a></li>
    <ul>
       @foreach($data['subcategories'] as $subcategory)
       <li><a href="category.html">{{$subcategory->category_name->name}}</li>
       @endforeach
    </ul>
    @endforeach

这篇关于在Laravel 5.6中将多个变量传递给一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆