2017-03-16 129 views
0

我有控制器,該控制器將加載視圖部分如何在laravel View中調用方法?

public function homePage(){ 
    $categories = Category::all(); 
    $products = Product::all(); 
    return view('cart.index',compact('categories','products')); 
} 

這裏是視圖(基本上檢查子類別是否存在,它會顯示否則跳過)

<ul class="list-group list-group-bordered list-group-noicon uppercase"> 
    <li class="list-group-item active"> 
     @foreach($categories as $row) 
      <a class="dropdown-toggle" href="#">{{$row->name}}</a> 
      @if({{StapsController::checkSubcategory($row->id)}}) 
       @foreach($subcategories as $sub) 
        <ul>           
         <li><a href="#"><span class="size-11 text-muted pull-right">(123)</span> {{$sub->s_name}}</a></li>   
        </ul> 
       @endforeach 
      @else 
       <li class="list-group-item"><a href="#"><span class="size-11 text-muted pull-right">(189)</span> {{$row->name}}</a></li> 
      @endif 
     @endforeach 

    </li> 
</ul> 

這裏是checkSubcategory方法

public function checkSubcategory($id){ 
    $category = Category::find($id); 
    $id = $category->id; 
    $subcategories = DB::table ('subcategories') 
     ->join('categories_subcategories','subcategories.id','=','categories_subcategories.subcategory_id') 
     ->join('categories','categories_subcategories.category_id','=','categories.id') 
     ->where('categories.id','=',$id) 
     ->select('subcategories.name as s_name ') 
     ->get(); 

    return $subcategories; 
} 

但我得到語法在視圖中調用方法的行錯誤? 什麼可能是錯誤..在視圖部分有什麼錯誤調用方法?

+0

'StapsController :: checkSubcategory($ id)'不是一個靜態函數,所以你不能像這樣調用它。 – btl

回答

0

如果checkSubcategory的邏輯在控制器本身中正常工作,則必須將其傳輸到模型。即讓模型的方法,以便從視圖中,你可以做到類似如下:

.... 
<a class="dropdown-toggle" href="#">{{$row->name}}</a> 
             @if({{$row->checkSubcategory($row->id);}}) 
             <ul> 
.... 

換句話說,定義你的Category模型此方法。

+1

啊,57秒前。 – Danny

+0

如何調用'$ row-> checkSubcategory()'函數? –

+0

@HIteshTank在模型中定義它作爲類的方法。 – SaidbakR

0

將你的功能放在其他地方,而不是控制器。在你的情況下,在Category模型,也許?

然後,在視圖中,您將能夠使用您的函數是這樣的:

$row->checkSubcategory($row->id) 

無需進行函數靜態的。

0

使用像這樣的關係和通過對象調用。

public function checkSubcategory() 
{ 
    return $this->hasMany('App\subcategories'); 
} 
0

有可能做到這一點easily-

  1. 自定義輔助函數

自定義輔助函數 我個人更喜歡你,因爲你可以使用自定義的輔助函數的一種方式在您的項目的任何地方調用此函數。

  • 在您應用程序/ HTTP目錄下創建一個helpers.php文件,並添加 的功能。在自動加載塊中,添加"files": ["app/Http/helpers.php"]
  • 運行作曲家dump-autoload

現在,無論你在helpers.php文件中寫什麼,都可以輕鬆訪問。

**注:請記住,在helpers.php文件,所有的代碼需要根據功能來定。不要在這個文件中使用class。

0

在視圖內調用你的方法並不是一個好習慣。在這種情況下,可以創建一個幫助文件並使用composer加載文件,也可以使用查詢範圍。試一試。使用查詢範圍

首先將其添加到您的類別模型中。

function scopeCheckSubcategory(){ 
     DB::raw('(SELECT subcategories.name as s_name 
     FROM `subcategories` 
     INNER JOIN categories_subcategories on categories_subcategories.subcategory_id = subcategories.id 
     AND categories_subcategories.category_id = categories.id') AS s_name' 
    ) 
} 

現在所說的範圍

$categories = Category::CheckSubcategory->get(); 

現在您在您的刀片文件

@if($row->s_name) 
... 
... 
@endif 
+0

在哪裏調用範圍? – User57

+0

與您的查詢。這樣Category :: CheckSubcategory-> get();請閱讀https://laravel.com/docs/5.4/eloquent#query-scopes –

0

有在刀片的幾個問題,在這裏被修正版本,

<ul class="list-group list-group-bordered list-group-noicon uppercase"> 
    <li class="list-group-item active"> 
     @foreach($categories as $row) 
      <a class="dropdown-toggle" href="#">{{$row->name}}</a> 
      <ul> 
       @if(StapsController::checkSubcategory($row->id)) 
        <li> 
         <a href="#"> 
          <span class="size-11 text-muted pull-right">(123)</span> {{$sub->s_name}} 
         </a> 
        </li> 
       @else 
        <li class="list-group-item"> 
         <a href="#"> 
          <span class="size-11 text-muted pull-right">(189)</span> {{$row->name}} 
         </a> 
        </li> 
       @endif 
      </ul> 
     @endforeach 
    </li> 
</ul> 
  1. @if({{StapsController::checkSubcategory($row->id);}})此行是錯誤的(語法錯誤
  2. 你已經寫了兩所endforeach陳述
  3. if statement在後endforeach聲明關閉
  4. 您在else statement

檢查上文有missing ul標籤我希望這有幫助。

+0

,那麼您將如何使用'{{$ sub> s_name}}' – User57

+0

您是否缺少一個foreach循環? @if(StapsController :: checkSubcategory($ row-> id)) 我認爲這行是@foreach(StapsController :: checkSubcategory($ row-> id)),$ sub是這個foreach循環的實例,如果我我錯了 – rahulsm

+0

我認爲你沒有把你的代碼中的foreach循環.. – User57

相關問題