2017-04-07 26 views
2

這很難解釋我的問題。我希望你能理解它。如何在laravel中的一個函數中返回同一路徑的多個視圖

對於我的項目我有一個主佈局,其中包含內容的標題和產量。

master.blade.php

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

現在主要的大衆路線我raturning的方法。

Web.php

Route::get('/', [ 
    'uses' => '[email protected]', 
    '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]); 
    } 
} 

現在我的查詢, '主要類別頁' 在主內容部分收益。但是我也想在頭文件中使用Mainadtype的相同數據。所以我想在同一個函數上返回標題的視圖。如果你瞭解它,請幫助。

什麼,我想現在是,

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]); 
    } 

但我知道,我們不能以這種方式回來。 在此先感謝。

回答

1

您也可以訪問標題中的變量。只需檢查變量是否存在,以便在加載其他視圖時不會出錯。

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

這樣,當您在索引頁上時,您的數據將可用。

如果您希望數據在所有視圖中均可用,則可以使用laravel view composer。來自官方文檔:

視圖編輯器是回調或類方法,在呈現 視圖時調用。如果您有要綁定到每個視圖渲染時間視圖 數據,將View Composer可以幫助您組織 這種邏輯到一個位置

希望它能幫助。

+0

thanxx很多先生!這樣可行!!!!!我非常努力的想要得到這個。非常感謝你:) –

+0

很高興我能幫上忙。 –

0

函數得到終止,因爲它獲取第一個返回語句,並且還有兩個視圖不能在laravel中返回,您可以做的唯一可能的事情是在佈局模板中包含視圖。例如在模板中添加類別視圖。

@if(showStore) 
@include('store.category') 
@endif 
相關問題