2017-10-12 35 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()以获取当前经过身份验证的用户标识。