1

Using Laravel 5.3, I have a database consisting of users, products and wishlists joined bya a pivot table product_wishlist. The schema is below:

USERS          WISHLISTS     PRODUCTS        PRODUCT_WISHLIST
-------------  -------------  --------------  ---------------
id             id             id              product_id
name           name           name            wishlist_id
               user_id        

In App/Wishlist.php

public function products()
{
    return $this->belongsToMany(Product::class, 'product_wishlist');
}

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

In App/product.php

public function wishlists()
{
    return $this->belongsToMany(Whishlist::class, 'product_wishlist');
}

A user can create wishlists and add products to wishlists.

In a product page, I'm trying show all wishlists of logged user.

$wishlists = Auth::user()->wishlists;

But i also want retrive if each wishlist contains the product (i will add a "add to wishlist" button near of each wishlist only if this not contains the product yet).

I edit after the Kabelo answer:

What i want is show a single product page, and in the sidebar shows all wishlists of the logged user (each user has your own wishlists): wishlist1 (add to list), wishlist2 (add to list),... but if for example the product of this page is already in the wishlist2 then no show "(add to list)". In these cases I could show "(remove from list)"

1
  • You are missing a relationship between whitelist and product. With that you just do something like $user->wishlist->products... and of course you can count them and make checks Commented Nov 21, 2016 at 12:02

1 Answer 1

2

A user can create wishlists and add products to wishlists.

From your question, I see a user can create multiple wishlists, here's a way to achieve that.

In App/Wishlist.php

class Wishlist extends Model
{
    /**
     * Get all of the products for the wishlist.
     */
    public function products()
    {
        return $this->belongsToMany('App\Product', 'product_wishlist');
    }

    /**
    * Get owner/creator of wishlist.
    */
    public function owner()
    {
        return $this->belongsTo('App\User');
    }

}

In App/Product

class Product extends Model
{
    /**
     * Get the wishlists for the product.
     */
    public function wishlists()
    {
        return $this->belongsToMany('App\Wishlist', 'product_wishlist');
    }

}

In Controller function

$products = App\Products::with(['wishlist'=> function($query){
    $query->where('user_id', Auth::user()->id);
}])->get();
return view('product.index', compact('products'))

In views/product/index.blade.php

@foreach( $products as $product )

    <div class="product">
        <h3>{{ $product->name }}</h3>
        @if( ! count($product->wishlists) )
            <button>Add to wishlist</button>
        @endif
    </div>

@endforeach



To show on single product page

In Controller

public function show($id){
    $_product = App\Products::where('id', $id)->first();
    $wishlists = null;
    $product_is_in_wishlist = false;
    if(Auth::check()){
        $wishlists = App\Wishlists::where('user_id', Auth::user()->id)
            ->with('products')->get();
        foreach( $wishlists as $wishlist ){
            if( $wishlist->products->where('id', $_product->id)->count() ){
                $product_is_in_wishlist= true;
            }
        }
    }

    return view('product.show', compact('_product', 'wishlists', 'product_is_in_wishlist'));
}

In view/product/show.blade.php

<div id='product_container'>
    <h1>{{ $_product->name }}</h1>
    @if( $product_is_in_wishlist )
        <button>Remove from wishlist</button>
    @else
        <button>Add to wishlist</button>
    @endif
</div>
<div id='wishlists_sidebar'>
    <ul>
        @foreach( $wishlists as $wishlist)
            <li>{{ $wishlist->name }}</li>
        @endforeach
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Kabelo. What i want is show a single product page, and in the sidebar shows all wishlists of the logged user (each user has your own wishlists): wishlist1 (add to list), wishlist2 (add to list),... but if for example the product of this page is already in the wishlist2 then no show "(add to list)". In these cases I could show "(remove from list)"
I just made added the 'add to wishlist' button logic. Let me know ho it goes.
works great thank you! I replaced if( $wishlist->products->where('id', $_product->id) ) with if( $wishlist->products->where('id', $_product->id)->count() )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.