vendredi 14 juin 2019

How to query Seller Id from Products table in database Laravel

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.

Here is my code.

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

 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');
 }
 }

My store Function

 public function store(Request $request)
{
    //Insert into orders table
    $user = User::find($id);
    $order = Order::create([
        'buyer_id' => auth()->user() ? auth()->user()->id : null,
        'seller_id' => Products_model::where("seller_id", "=", $user->id)->get(),
        'shipping_email' => $request->email,
        'shipping_name' => $request->name,
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
       // 'error' => null,
    ]);

I was expecting to get seller_id(the one who listed the product) and buyer_id. Any help will be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire