Hello I have Post model with relation:
public function category()
{
return $this->belongsTo(category::class);
}
I need show posts on every tab of category. For this I need use groupBy. When I do this:
$posts = Post::with('category')->groupBy('category.title')->get();
I get error:
Column not found: 1054 Unknown column 'category.title'.
Why? How I can return my posts with key of category title?
For multilangual I use this package: https://github.com/spatie/laravel-translatable
Try Collection's group-by method:
$posts = Post::with('category')->get()->groupBy('category.title')->all();
You may pass a callback to return the value you wish to key the group by (as you mentioned you are using laravel-translatable package):
$posts = Post::with('category')->get()->groupBy(function ($post, $key) {
return $post->category->getTranslation('title', 'fr');
})->all();