Trouble rendering my variables to my view

时间:2017-10-12 09:47:34

标签: laravel

For some reason I can't seem to render these variables to my index. Here is my code on my SellerController.

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

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

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

and here is my code on my view

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

     @endforeach
</ul>

2 个答案:

答案 0 :(得分:1)

Use get() to execute the query:

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

And you don't need to pass $id to a view since you can just use auth()->id() directly in the view to get currently authenticated user ID.

答案 1 :(得分:1)

You should try this:

1) You need to update your controller function like:

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

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

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

OR

2) You need to update your view file like:

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


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

        @endforeach
  </ul>
相关问题