Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aminbaig's avatar

Displaying data for an intermediary table

I have three table: user, products and orders. A user can subscribe/unsubscribe to multiple products, each subscription is stored in the orders table which has user_id and product_id fields.

Following are my relationships in user Model:

    public function orders()
    {
      return $this->hasMany('App\Order');
    }

    public function userOrders()
    {
      return $this->belongsToMany(Product::class, 'orders');
    }

Products Model:

    public function orders()
    {
      return $this->belongsTo('App\Order');
    }

Orders Model:

    public function user()
    {
      return $this->belongsTo('App\User');
    }

    public function products()
    {
      return $this->hasMany('App\Product');
    }

In my HomeController I am passing the values to a view:

    public function index()
    {
        $user = Auth::user()->id;
        $products = Product::all();
        $orders = Order::where('user_id', Auth::user()->id)->get();
      //  dd($orders);
        return view('home')->withOrders($orders)->withProducts($products)->withUser($user);
    }

In my view I want to show the list of all the products to which the user has "subscribed" all of these products are in my orders pivot table, I can get the user_id and product_id to show in my view but I am utterly confused on how to show the product name and other details as well. The home view is supposed to show All the products to which the logged in user has subscribed. Here is my view:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif

                    <h2>Your subscribed Courses:</h2>
                    @foreach ($orders as $order)
                      sdfdfd
                      <p>{{$order}}</p>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Any help much appreciated guys.

0 likes
3 replies
lyleyboy's avatar

If I understand you correctly I understand this one can be a doozy and there isn't much in the way of documentation on it.

Have a look into the PIVOT method

echo $role->pivot->created_at; <- Straight from the docs.

You should be able to dig into the pivot data using that.

aminbaig's avatar

Ok....I have modified my models for many to many relationships:

user models is now:

    public function products()
    {
      return $this->belongsToMany('App\Product', 'orders', 'user_id', 'product_id')->withPivot('name', 'price')->withTimestamps();
    }

    public function orders()
    {
      return $this->hasMany('App\Order');
    }

Products Model:

    public function orders()
    {
      return $this->belongsToMany('App\Order', 'orders', 'product_id', 'user_id');
    }

and order model:

    public function user()
    {
      return $this->belongsTo('App\User');
    }

    public function products()
    {
      return $this->hasMany('App\Product');
    }

How should i modify my foreach statement in my view to display product name from the order table for the currently logged in user which is being passed from my controller:

    public function index()
    {
      //  $user = Auth::user()->id;
        $products = Product::all();
        $orders = Order::where('user_id', Auth::user()->id)->get();
      //  dd($orders);
        return view('home')->withOrders($orders)->withProducts($products);
    }

my current foreach loop:

        @foreach ($orders->products as $product)
                       list of products
                      <p>{{$product->orders->name}}</p>
                    @endforeach 

Please help. I am getting this error:

Property [products] does not exist on this collection instance. (View: /home/vagrant/Code/mysubs/resources/views/home.blade.php)

Please or to participate in this conversation.