Merge 'with' and 'whereHas' in Laravel 5

前端 未结 3 1901
清歌不尽
清歌不尽 2020-11-29 05:37

I have this code in Laravel 5, using Eloquent, which is working perfectly:

$filterTask = function($query) use ($id) {
    $query->where(\'taskid\', $id);
         


        
相关标签:
3条回答
  • 2020-11-29 05:50

    The 'macroable' way (Laravel 5.4+)

    Add this inside a service provider's boot() method.

    \Illuminate\Database\Eloquent\Builder\Eloquent::macro('withAndWhereHas', function($relation, $constraint){
        return $this->whereHas($relation, $constraint)->with([$relation => $constraint]);
    });
    
    0 讨论(0)
  • 2020-11-29 05:55

    I want to extend the answer from @lukasgeiter using static functions.

    public static function withAndWhereHas($relation, $constraint){
        return (new static)->whereHas($relation, $constraint)
            ->with([$relation => $constraint]);
    }
    

    Usage is the same

    User::withAndWhereHas('submissions', function($query) use ($id){
        $query->where('taskid', $id);
    })->get();
    
    0 讨论(0)
  • 2020-11-29 06:05

    In terms of performance you can't really optimize anything here (except if you were to move from eloquent relations to joins). With or without whereHas, two queries will be run. One to select all users another one to load the related models. When you add the whereHas condition a subquery is added, but it's still two queries.

    However, syntactically you could optimize this a bit by adding a query scope to your model (or even a base model if you want to use this more often):

    public function scopeWithAndWhereHas($query, $relation, $constraint){
        return $query->whereHas($relation, $constraint)
                     ->with([$relation => $constraint]);
    }
    

    Usage:

    User::withAndWhereHas('submissions', function($query) use ($id){
        $query->where('taskid', $id);
    })->get();
    
    0 讨论(0)
提交回复
热议问题