查询Laravel的关系

I have an Order table that has buyer_id and seller_id as columns. The buyer_id is the current login user who is buying something from the seller. What I want is when the buyer is submitting the order, the seller_id of the product should also be present.

Here is how I'm creating the order in checkout controller inside the store function

//Insert into the orders table
$order = Order::create([
    'buyer_id' => auth()->user() ? auth()->user()->id : null,
    'seller_id' => //what should i put here to query the owner    of the product(the user who listed the product)
    'shipping_email' => $request->email,
    'shipping_name' => $request->name,
    'shipping_city' => $request->city,
    'shipping_phone' => $request->phone,
   // 'error' => null,
]);

User.php

<?php

namespace App;

 use Illuminate\Notifications\Notifiable;
 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Illuminate\Foundation\Auth\User as Authenticatable;

 class User extends Authenticatable
{
 use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'Seller'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token', 
];

//public function isSeller() {
 //   return $this->seller;
//}

public function products()
{
  return $this->hasMany(Products_model::class);
}
/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function orders()
{
    $this->hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
}

public function buys() {
    $this->hasMany(Order::class, 'buyer_id', 'id');
 }
 public function sells() {
     $this->hasMany(Order::class, 'seller_id', 'id');
  }
  }

Products_model.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class products_model extends Model
{
    protected $table='products';
    protected $primaryKey='id';
    protected $fillable=['seller_id','pro_name','pro_price','pro_info','image','stock','category_id'];
}

OrderProduct.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderProduct extends Model
{
    protected $table = 'order_product';
    protected $fillable = ['order_id', 'product_id', 'quantity'];

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

Order.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    //protected $table = 'orders';
    protected $fillable =  [
        'buyer_id', 'seller_id','shipping_email', 'shipping_name', 'shipping_city', 'shipping_phone', 'billing_subtotal', 'billing_total',
    ];

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

    public function products()
    {
        return $this->belongsToMany('App\Products_model')->withPivot('quantity');
    }

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

    public function buyer()
    {
        return $this->belongsTo(User::class, 'id', 'buyer_id');
    }

    public function seller()
    {
        return $this->belongsTo(User::class, 'id', 'seller_id');
    }
}

Seller view Order function

   public function viewOrders(User $user)
    {

        $products = Products_model::where('seller_id', '=', $user->id)->get();
        // all sells
        $sells = $user->sells;
        // all buys
        $buys = $user->buys;

    }
    //dd( $products);
    return view('orders')->with(compact('orders'));

Seller Dashboard blade

   @foreach($sells as $sell) 
  <tr>
  <td>{{$sell->orders}}</td>
  <td>{{$sell->products}}</td>
  @foreach($sell->orders as $order)
  <td>{{$order->created_at}}</td>
  <td>{{$order->shipping_name}}</td>
  <td>{{$order->shipping_city}}</td>
  <td>{{$order->shipping_phone}}</td>
  <td>
    <a href="">View Order Details</a>
  </td>
  </tr>

Your products table should have the relationship to the seller of the product, and in the order table you should have the product_id instead of the seller_id. Then when you create an order, you should put the product that the buyer is buying and the seller will be there through the product relationship. I hope I am clear and this is what you are looking for.

And even more, while I am thinking about it. I don't know your idea on the orders/products but I guess an order to a product should be a many to many relationship as most of the stores do. Meaning an order contains many products.

A seller has many orders through the products model. So if a product belongs to a seller, then you can make this in your Seller model:

public function orders()
{
    return $this->hasManyThrough(Order::class, Product::class);
}

You can read more on this here.