Check if belongsToMany relation exists - Laravel

Update: I did not take into account the usefulness of checking multiple relations, if that is the case then @deczo has a way better answer to this question. Running only one query to check for all relations is the desired solution.

    /**
     * Determine if a Client has a specific Product
     * @param $clientId
     * @param $productId
     * @return bool
     */
    public function clientHasProduct($clientId, $productId)
    {
        return ! is_null(
            DB::table('client_product')
              ->where('client_id', $clientId)
              ->where('product_id', $productId)
              ->first()
        );
    }

You could put this in you User/Client model or you could have it in a ClientRepository and use that wherever you need it.

if ($this->clientRepository->clientHasProduct($clientId, $productId)
{
    return 'Awesome';
}

But if you already have defined the belongsToMany relationship on a Client Eloquent model, you could do this, inside your Client model, instead:

    return ! is_null(
        $this->products()
             ->where('product_id', $productId)
             ->first()
    );

I think the official way to do this is to do:

$client = Client::find(1);
$exists = $client->products->contains($product_id);

It's somewhat wasteful in that it'll do the SELECT query, get all results into a Collection and then finally do a foreach over the Collection to find a model with the ID you pass in. However, it doesn't require modelling the pivot table.

If you don't like the wastefulness of that, you could do it yourself in SQL/Query Builder, which also wouldn't require modelling the table (nor would it require getting the Client model if you don't already have it for other purposes:

$exists = DB::table('client_product')
    ->whereClientId($client_id)
    ->whereProductId($product_id)
    ->count() > 0;

Alex's solution is working one, but it will load a Client model and all related Product models from DB into memory and only after that, it will check if the relationship exists.

A better Eloquent way to do that is to use whereHas() method.

1. You don't need to load client model, you can just use his ID.

2. You also don't need to load all products related to that client into memory, like Alex does.

3. One SQL query to DB.

$doesClientHaveProduct = Product::where('id', $productId)
    ->whereHas('clients', function($q) use($clientId) {
        $q->where('id', $clientId);
    })
    ->count();

The question is quite old but this may help others looking for a solution:

$client = Client::find(1);
$exists = $client->products()->where('products.id', $productId)->exists();

No "wastefulness" as in @alexrussell's solution and the query is more efficient, too.