jeudi 17 octobre 2019

How to display data from favoritesProduct pivot table in Laravel?

I'm creating a functionality where a user can favorites an item. This works and the item is added to the database. Now when i try to display the favorited item, it shows nothing and i don't know why.

Here is how i retrieve the data to show from the database

 public function favoriteslist(Request $request)

 {
    $products = Auth::user()->favouriteProducts()->get();
    return view('front.wishlist', compact('products'));
 }

This is the user relationship with favoritesProduct (User.php)

public function favouriteProducts(){
    return $this->morphedByMany(Product::class,'favouriteable')
    ->withPivot(['created_at'])->orderBy('pivot_created_at', 'desc');
 }

Product.php

  public function favourites(){
    return $this->morphToMany(User::class, 'favouriteable');
  }

  public function favouritedBy(User $user){
    return $this->favourites->contains($user);
  }

This is how the database table looks like https://imgur.com/a/SagRQ1V

How can i get it to show the list of favorites produts?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/35I4ylK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire