lundi 17 juin 2019

Displaying Eloquent relationships in Laravel

I'm trying to display orders to sellers but also buyers can be able to see their orders. I have created all the necessary relationships and the data is on the database. I'm stuck in trying to query this data from database so i can show it. I have these tables orders table, order_product table. Here is how they look like https://imgur.com/a/Ud9e2Hh

I have tried below functions but still no luck in solving the problem. If you need more information, just comment and i will provide.

Here are my functions

// Seller Orders 
 public function viewOrders(User $user)
 {
 // all sells
 $sells = $user->allOrderFromSellers();
 dd($sells);// this returns empty array
 return view('orders')->with(compact('sells'));
 }


 //Buyer Orders
 public function myOrders(User $user)
 { 

  return view('myOrders', compact('user','orders'));
  dd($orders);
 }

And here are the models.

 order_product.php

 <?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->belongsTo('App\Products_model');
 }

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

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

 public function order()
 {
  return $this->belongsTo(Order::class);
 }
 }

and this is 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()
 {
   return $this->hasManyThrough(Order::class,     Products_model::class, 'buyer_id', 'seller_id', 'product_id');
 }

 public function orderFromBuyers()
 {
  return $this->hasManyThrough(OrderProduct::class,    Products_model::class, 'buyer_id', 'product_id');
 }

 public function orderFromSellers()
 {
    return $this->hasManyThrough(OrderProduct::class, Products_model::class, 'seller_id', 'product_id');
 }

 public function allOrderFromBuyers()
 {
 return $this->hasMany(OrderProduct::class, 'buyer_id');
 }


 public function allOrderFromSellers()
 {
 return $this->hasMany(OrderProduct::class, 'seller_id');
 }

 }

the above functions give me this "Collection {#281 ▼ #items: [] }" please help me on this, i have spent so much time but nothing is working.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire