How to get relationship counts without loading objects in laravel

守給你的承諾、 提交于 2019-12-08 10:44:15

问题


I have a model customer and it has many projects. I want to find projects count without including its object.

Customer model includes:

public function numberOfProjects()
{
    return $this->hasMany(Project::class)->count();
}

Query in my controller:

 $customers = Customer::where(['is_active'=>1])
                                ->with(['customerContactInformation'=> function ($query) {
                                    $query->where('is_active',1);
                                }, 'numberOfProjects'])
                                ->skip($skip)->take(10)
                                 ->get();

Its giving me error:Call to a member function addEagerConstraints() on integer


回答1:


Try this

Customer Model

public function numberOfProjects()
{
    return $this->hasMany(Project::class);
}

Controller

$customers = Customer::where(['is_active'=>1])
                    ->with(['customerContactInformation'=> function ($query) {
                        $query->where('is_active',1);
                    }])
                    ->withCount('numberOfProjects') //you can get count using this
                    ->skip($skip)
                    ->take(10)
                    ->get();

That should be work

$customers = Customer::withCount('numberOfProjects')->get();

WithCount on the particular status

$customers = Customer::withCount([
                        'numberOfProjects',
                        'numberOfProjects as approved_count' => function ($query) {
                            $query->where('approved', true);
                        }
                    ])
                    ->get();



回答2:


class Tutorial extends Model
{
    function chapters()
    {
        return $this->hasMany('App\Chapter');
    }

    function videos()
    {
        return $this->hasManyThrough('App\Video', 'App\Chapter');
    }
}

And then you can do:

Tutorial::withCount(['chapters', 'videos'])

Counting Related Models If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models. For example:

$posts = App\Post::withCount('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_count;
}

You may add the "counts" for multiple relations as well as add constraints to the queries:

$posts = App\Post::withCount(['votes', 'comments' => function ($query) {
    $query->where('content', 'like', 'foo%');
}])->get();

echo $posts[0]->votes_count;
echo $posts[0]->comments_count;

You may also alias the relationship count result, allowing multiple counts on the same relationship:

$posts = App\Post::withCount([
    'comments',
    'comments as pending_comments_count' => function ($query) {
        $query->where('approved', false);
    }
])->get();

echo $posts[0]->comments_count;

echo $posts[0]->pending_comments_count;

If you're combining withCount with a select statement, ensure that you call withCount after the select method:

$posts = App\Post::select(['title', 'body'])->withCount('comments');

echo $posts[0]->title;
echo $posts[0]->body;
echo $posts[0]->comments_count;


来源:https://stackoverflow.com/questions/55390530/how-to-get-relationship-counts-without-loading-objects-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!