Level 75
$attributs = Tag::with('attributs')
->whereIn('slug', ['type', 'relationship', 'reason'])
->get()
->pluck('attributs');
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone, is there a way to run a single query instead of three like this in the same model?
$type = Tag::with('attributs')
->where([['slug', '=', 'type']])
->get()->pluck('attributs');
$relationship = Tag::with('attributs')
->where([['slug', '=', 'relationship']])
->get()->pluck('attributs');
$reason = Tag::with('attributs')
->where([['slug', '=', 'reason']])
->get()->pluck('attributs');
Use groupBy for that
$attributs = Tag::with('attributs')
->whereIn('slug', ['type', 'relationship', 'reason'])
->get()
->pluck('attributs')
->groupBy('slug');
$type = $attributs['type'] ?? [];
$relationship = $attributs['relationship'] ?? [];
$reason = $attributs['reason'] ?? [];
Docs: https://laravel.com/docs/8.x/collections#method-groupby
Please or to participate in this conversation.