Search for relationship Laravel whereHas?

4

I have a Query in my Criteria that returns the id belonging to relationship between 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 portals relationship in models. Is there something like ?

    
asked by anonymous 04.10.2016 / 21:47

1 answer

3

Yes, you can only put <> in where

Example

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

Also exists how to compare with whereIn / whereNotIn

Examples:

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

In your case, too, whereNotIn

whereRaw can also satisfy this search:

Example

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

In the most recent version you can use whereDoesntHave

Example:

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