PHP e-commerce order split to sellers Laravel
<p>I'm currently building a little webshop using Laravel 5.3, where users can register and add products to sell. Another user can then search for products, putt them in the shopping cart and buy those. Then I save this order information into the db (as 2 tables), <code>order</code> (containing the id (of the order), the user_id (who bought it), and the total paid amount) and then theres <code>order_items</code> (containing the id, the relating order id and the product id).</p>

<p>Now I want to give the seller as well as the buyer the possibility to view the past orders. Now comes the clue: Showing this to the buyer is as easy as could be, because I just need to check for the user id in the <code>orders</code>table and get those. But seller is not that easy, after I thought about, because the seller should also see "his" orders, so the orders from buyers, that contain one or more of his products, BUT: he should only see the part of the order with his products and not the products the buyer propably bought from somebody else. I think you know what I mean?</p>

<p>Is there any way to solve this "easily" or a best practice how this is done? 
Basically I could just create two subtables, called <code>sub_orders</code> and <code>sub_order_items</code> or something and then create a <code>sub_order</code> for every seller (creating this right after creating the order), but this is not too easy to implement (I guess) and maybe there's a better way I'm not thinking of yet.</p>

<p>Are there any ideas?</p>

<p>Those are all relevant models:</p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    public function user()
    {
        return $this-&gt;belongsTo('App\User');
    }

    public function orderItems()
    {
        return $this-&gt;hasMany('App\OrderItem');
    }
}
</code></pre>

<p>and</p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
    protected $table = 'order_items';

    public function product()
    {
        return $this-&gt;belongsTo('App\Product');
    }
}
</code></pre>

<p>and</p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    //
}
</code></pre>

<p><strong>Edit</strong>: This is what I got so far:</p>

<pre><code>$orders = Order::whereHas('orderItems.product', function ($query) {
            $query-&gt;where('user_id', '=', \Auth::user()-&gt;id);
        })-&gt;get();

        return view('order.list',['orders'=&gt;$orders]);
</code></pre>

<p>So this lists all orders that contain at least one product from the seller. When I know (in that view <code>order.list</code>) click on one order I can see all the products of the order, but I just want to see those who are from this seller. 
This is how I show a single order:</p>

<pre><code>public function viewOrder($orderId){
        $order = Order::find($orderId);
        return view('order.view',['order'=&gt;$order]);
    }
</code></pre>

<p>How can I now only show relevant products of the order and the sum of those relevant products only?</p>