2017-10-12 32 views
2

出於某種原因,我似乎無法將這些變量渲染到我的索引。 這是我的SellerController上的代碼。麻煩渲染我的變量到我的視圖

public function index() 
{ 
    $id = Auth::user()->id; 

    $product = Product::where('user_id', $id); 

    return view('seller.index', compact('product','id')); 
} 

,這裏是我的看法

<ul> 
    @foreach($product as $p) 
     <li> 
      <p>{{ $p->description }}</p> 
      <p>{{ $p->price }}</p> 
     </li> 

    @endforeach 
</ul> 

回答

1

你應該試試這個:

1)你需要更新你的controller功能,如:

public function index() 
    { 
    $id = Auth::user()->id; 

    $product = Product::where('user_id', $id)->get(); 

     return view('seller.index', compact('product','id')); 
    } 

OR

2)你需要更新你的看法文件如:

<ul> 
     @foreach($product->get() as $p) 
     <li> 


     <p>{{ $p->description }}</p> 
     <p>{{ $p->price }}</p> 
     </li> 

     @endforeach 
    </ul> 
1

使用get()我的代碼來執行查詢:

$products = Product::where('user_id', $id)->get(); 

而你並不需要通過$id的觀點,因爲你可以直接在視圖中使用auth()->id()以獲取當前經過身份驗證的用戶標識。