lundi 9 mars 2020

Cannot get details of data from product table that are added to wishlist table

I want the details of product that are added to wishlist table. I'm getting the data from wishlist table but i want to establish relation from product and user model also so that i can get the details of the product. My table Structure is :

Wishlists table:

id | product_id | user_id 

products table :

id | name | price 

users table :

id  | first_name  | last_name | contact 

Wishlist Model :

public function user(){
   return $this->belongsTo(User::class);
}

public function product(){
   return $this->belongsTo(Product::class);
}

Product Model :

public function wishlist(){
    return $this->hasMany(Wishlist::class);
 }

 public function users()
{
    return $this->belongsToMany(User::class);
}

Users Model :

public function wishlist(){
    return $this->hasMany(Wishlist::class);
 }

I'm getting data from wishlist table through this code but can't get data from product table :

return Wishlist::with('product')->where('user_id', auth()->id())->get();  

product relation is returning null



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire