Search code examples
phplaraveleloquente-commercelaravel-5.3

PHP e-commerce order split to sellers Laravel


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), order (containing the id (of the order), the user_id (who bought it), and the total paid amount) and then theres order_items (containing the id, the relating order id and the product id).

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 orderstable 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?

Is there any way to solve this "easily" or a best practice how this is done? Basically I could just create two subtables, called sub_orders and sub_order_items or something and then create a sub_order 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.

Are there any ideas?

Those are all relevant models:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

and

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

and

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    //
}

Edit: This is what I got so far:

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

        return view('order.list',['orders'=>$orders]);

So this lists all orders that contain at least one product from the seller. When I know (in that view order.list) 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:

public function viewOrder($orderId){
        $order = Order::find($orderId);
        return view('order.view',['order'=>$order]);
    }

How can I now only show relevant products of the order and the sum of those relevant products only?


Solution

  • Not sure how you named your Models, but here's the idea. Something like:

    // get the logged in seller and set it to $seller
    
    $orders = Order::whereHas('orderItems.product', function ($query) {
        $query->where('seller_id', '=', $seller->id);
    })->get();
    
    // will get you orders that have products by this seller
    

    From that point on, you can filter what you want to show (exclude products in the order by different sellers, etc.)