dimanche 1 août 2021

Count data slow use relationship in laravel?

$user = User::select(['id', 'name'])
            ->withCount([
                'posts as success' => function ($query) {
                    $query->where('status', 0);
                },
                'posts as error' => function ($query) {
                    $query->whereIn('status', [1, 2, 3]);
                },
            ])                
            ->where('order', 3)
            ->get();

Model Post.php :

public function users()
    {
        return $this->belongsTo(User::class);
    }

Model User.php :

public function posts()
    {
        return $this->hasMany(Post::class);
    }

I want to count status in post table through relationship. Like the above code, I got the result I wanted. But it is very slow, about 10 seconds or more. Is there any way to fix it. My post table has 400,000 data



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2WM77TR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire