2017-11-11 9 views
1

使用控制器傳遞多個變量視圖這是我的控制器如何laravel

public function show($id) 
{ 
    $products = Item::find($id)->products; 

    $categories = Item::all(); 

    return view('category.index', compact(['categories', 'products'])); 
} 

這是我的看法

@extends('layout.front') 
@section('page') 
<div class="row" id="portfolio"> 

<!-- Page Content --> 
<div class="container"> 

    <div class="row"> 

     <div class="col-lg-3" style="margin-top:20px;"> 

      <nav class="main-nav"> 
       <ul class="main-nav-ul"> 
        <li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li> 
        <li><a href="#" style="font-weight: 600;">HouseHold<span></span></a> 
         <ul> 
          @if(!empty($categories)) 
           @forelse($categories as $category) 
           <li><a href="{{ route('category.show', $category->id)}}">{{ $category->name }}</a></li> 

           @empty 
            <li>No data found</li> 
           @endforelse 
          @endif 
         </ul> 
        </li> 
        <li><a href="#" style="font-weight: 600;">Vegetables</a></li> 
        <li><a href="#" style="font-weight: 600;">Fruits</a></li> 
       </ul> 
      </nav> 

     </div> 
     <!-- /.col-lg-3 --> 

     <div class="col-lg-9"> 


      <div class="row" style="margin-top:20px;"> 



        @foreach($products as $key=>$product) 
         <div class="col-md-4 col-sm-6 portfolio-item" id="items"> 
          <div class="card h-100"> 

           <a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a> 
           <div class="card-body"> 
            <h4 class="card-title"> 
             <a href="#"><br />{{ $product->item_name}}</a> 
            </h4> 
            <p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p> 
            <h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4> 

           </div> 
          </div> 
         </div> 
        @endforeach 


      </div> 
      <!-- /.row --> 

     </div> 
     <!-- /.col-lg-9 --> 

    </div> 
    <!-- /.row --> 

</div> 
<!-- /.container --> 
</div> 
@endsection 

我想通過在同我的類別列表和產品項目視圖。但是我無法將這兩個變量從控制器傳遞到我的視圖。它顯示了這個錯誤。

未定義的變量:產品(查看:/var/www/html/hzone_new/resources/views/category/index.blade.php)

回答

1

更新您的控制器功能,如:

$categories = Item::all(); 
$products = Item::find($id)->products; 
return view('category.index', compact('categories', 'products')); 

注意:你不能定義你的函數變量的產品定義,請

@if(!empty($products)) 
    @foreach($products as $key=>$product) 
         <div class="col-md-4 col-sm-6 portfolio-item" id="items"> 
          <div class="card h-100"> 

           <a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a> 
           <div class="card-body"> 
            <h4 class="card-title"> 
             <a href="#"><br />{{ $product->item_name}}</a> 
            </h4> 
            <p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p> 
            <h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4> 

           </div> 
          </div> 
         </div> 
        @endforeach 
@endif 
+0

感謝您支持我兄弟。你的代碼工作正常。非常感謝 –

+0

@ K.Mihiranga:很高興幫助! –