Search for relationship whereHas Laravel?

Asked

Viewed 1,650 times

4

I have a Query in my Criteria that returns the id belonging to the relationship amid models

if ($this->request->has('notin_portal')) 
{
    $portal = $this->request->get('notin_portal');
    if ($this->request->get('notin_portal')) 
    {
         $model = $model->whereHas('portals', function($query) use ($portal)
         {
              $query->where('id', $portal);
         });
         return $model;
    }
}

My problem is that I need to return the ids that do not belong to the relationship portals in models. Is there anything like?

1 answer

3


Yes just put it <> in the where

Example

$query->where('id', '<>', $portal);

Also exists the way to compare with whereIn / whereNotIn

Examples:

->whereIn('id', [1, 2, 3]);

->whereNotIn('id', [1, 2, 3]);

in your case would also work whereNotIn

With whereRaw may also satisfy this search:

Example

->whereRaw("id <> {$portal}");

In the latest version you can use whereDoesntHave

Example:

$model = $model->whereDoesntHave('portals', function ($query) use ($portal)
{
    $query->where('id', $portal);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.