title: How to query Seller Id from Products table in database Laravel
body: <p>I'm working on a project where users can sell and also buy products, and in my database there are two tables(orders and order products table)in orders tables there's a buyer_id and seller_id. So if a user buys product it shows buyer_id now the problem comes to seller_id. It doesn't show the seller_id.</p>
  
  <p>Here is my code.</p>
  
  <p>User.php</p>
  
  <pre><code> 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-&gt;seller;
  //}
  
   public function products()
   {
    return $this-&gt;hasMany(Products_model::class);
   }
  /**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
  protected $casts = [
      'email_verified_at' =&gt; 'datetime',
   ];
  
   public function orders()
   {
     return $this-&gt;hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
    }
  
   public function orderFromBuyers()
   {
    $this-&gt;hasManyThrough(OrderProduct::class, Products_model::class, 'buyer_id', 'product_id');
   }
  
   public function orderFromSellers()
   {
      $this-&gt;hasManyThrough(OrderProduct::class, Products_model::class, 'seller_id', 'product_id');
   }
   }
  </code></pre>
  
  <p>Products_model.php</p>
  
  <pre><code> &lt;?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'];
  }
  </code></pre>
  
  <p>OrderProduct.php</p>
  
  <pre><code>&lt;?php
  namespace App;
  use Illuminate\Database\Eloquent\Model;
  
  class OrderProduct extends Model
  {
  protected $table = 'order_product';
  protected $fillable = ['order_id', 'buyer_id', 'seller_id','product_id', 'quantity'];
  
  public function products()
  {
    return $this-&gt;belongsTo('App\Products_model');
  }
  
  public function buyer()
  {
      return $this-&gt;belongsTo(User::class, 'id', 'buyer_id');
  }
  
  public function seller()
   {
      return $this-&gt;belongsTo(User::class, 'id', 'seller_id');
   }
    }
  </code></pre>
  
  <p>Order.php</p>
  
  <pre><code> &lt;?php
   namespace App;
   use Illuminate\Database\Eloquent\Model;
   class Order extends Model
   {
  //protected $table = 'orders';
  protected $fillable =  [
      'shipping_email', 'shipping_name', 'shipping_city', 'shipping_phone', 'billing_subtotal', 'billing_total',
  ];
  
  public function user()
  {
      return $this-&gt;belongsTo('App\User');
  }
  
  public function products()
  {
      return $this-&gt;belongsToMany('App\Products_model')-&gt;withPivot('quantity');
  }
  
   public function orders(){
       return $this-&gt;hasMany('App\OrderProduct', 'order_id');
   }
  </code></pre>
  
  <p>My store Function</p>
  
  <pre><code>  public function store(Request $request)
     {
      //Insert into orders table
      $order = Order::create([
          'buyer_id' =&gt; auth()-&gt;user() ? auth()-&gt;user()-&gt;id : null,
          'shipping_email' =&gt; $request-&gt;email,
          'shipping_name' =&gt; $request-&gt;name,
          'shipping_city' =&gt; $request-&gt;city,
          'shipping_phone' =&gt; $request-&gt;phone,
         // 'error' =&gt; null,
      ]);
  
      //Insert into order product table
      if ($order) {
          foreach(session('cart')  as $productId =&gt;$item) {
             if (empty($item)) {
                 continue;
             }
             OrderProduct::create([
              'order_id' =&gt; $order-&gt;id ?? null,
              'product_id' =&gt; $productId,
             // $products=DB::table('products')-&gt;where('id',$id)-&gt;get();
              'quantity' =&gt; $item['quantity'],
              //dd($item)
          ]);
         }
      }
  </code></pre>
  
  <p>CheckoutController(function)</p>
  
  <pre><code>    public function store(Request $request)
       {
      //Insert into orders table
      $order = Order::create([
          'buyer_id' =&gt; auth()-&gt;user() ? auth()-&gt;user()-&gt;id : null,
          'seller_id' =&gt; auth()-&gt;user() ? auth()-&gt;user()-&gt;id : null,            'shipping_email' =&gt; $request-&gt;email,
          'shipping_name' =&gt; $request-&gt;name,
          'shipping_city' =&gt; $request-&gt;city,
          'shipping_phone' =&gt; $request-&gt;phone,
         // 'error' =&gt; null,
      ]);
  
      //Insert into order product table
      if ($order) {
          foreach(session('cart')  as $productId =&gt;$item) {
             if (empty($item)) {
                 continue;
             }
             OrderProduct::create([
              'order_id' =&gt; $order-&gt;id ?? null,
              'product_id' =&gt; $productId,
             // $products=DB::table('products')-&gt;where('id',$id)-&gt;get();
              'quantity' =&gt; $item['quantity'],
              //dd($item)
          ]);
         }
      }
  
     //Empty Cart After  order created
      $cart = session()-&gt;remove('cart');
       return redirect()-&gt;route('confirmation.index')-&gt;with('success_message', 'Thank you! Your payment has been successfully accepted!');
       }
  </code></pre>
  
  <p>ProductController(function)</p>
  
  <pre><code>    public function viewOrders(User $user)
    {
  
          $products = Products_model::where('seller_id', '=', $user-&gt;id)-&gt;get();
          // all sells
          $sells = $user-&gt;sells;
          // all buys
          $buys = $user-&gt;buys;
  
      }
      //dd( $products);
      return view('orders')-&gt;with(compact('orders'));
  </code></pre>
  
  <p>My View File(blade)</p>
  
  <pre><code>  @foreach($sells as $sell) 
  &lt;tr&gt;
    &lt;td&gt;{{$sell-&gt;orders}}&lt;/td&gt;
    &lt;td&gt;{{$sell-&gt;products}}&lt;/td&gt;
    @foreach($sell-&gt;orders as $order)
    &lt;td&gt;{{$order-&gt;created_at}}&lt;/td&gt;
    &lt;td&gt;{{$order-&gt;shipping_name}}&lt;/td&gt;
    &lt;td&gt;{{$order-&gt;shipping_city}}&lt;/td&gt;
    &lt;td&gt;{{$order-&gt;shipping_phone}}&lt;/td&gt;
    &lt;td&gt;
      &lt;a href=""&gt;View Order Details&lt;/a&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  @endforeach
  @endforeach
  </code></pre>