dousha1831 2017-04-07 06:02
浏览 46
已采纳

如何在同一路径的laravel中的一个函数中返回多个视图

it is quite difficult to explain my question. I hope you will be able understand it.

For my project I have a master layout which contains the header and yield of content.

master.blade.php

 @include('partials.header')
    <div class="container-fluid main-con">
        @yield('content')
        @include('partials.footer')
    </div>  

now on main public route I am raturning a method.

Web.php

Route::get('/', [
    'uses' => 'ProductContoller@getIndex',
    'as' => 'product.mainCats'
]);

ProductContoller.php

class ProductContoller extends Controller
{

    public function getIndex(){
        $ad_cats = Mainaddtype::orderBy('title')->get();
        return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
    }
}

Now my query is, 'main-categories-page' is yielding in content section of master. but I want the same data of Mainadtype in header also. So I want to return view of header on same function. Please help if u understand it.

what I am trying now is,

 public function getIndex(){
        $ad_cats = Mainaddtype::orderBy('title')->get();
        return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
        return view( 'partials.header', ['mediacats' => $ad_cats]);
    }

But I know we cant return in this way. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duanruoyu6675 2017-04-07 06:22
    关注

    You can access the variable in the header as well. Just have a check to see if the variable exists so that you don't get errors when you load other views.

    @if(!empty($mediacats))
       // do awesome stuffs with $mediacats
    @endif
    

    This way your data will be available when you are on index page.

    If you want the data to be available in all views, you can use laravel view composer. From official docs:

    View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题