I have got two tables articles and tags in the database. My goal was to retrieve all fields from articles table and one category field from tags table with Eloquent. I couldn't do this so instead I came up with the following method in my controller.
public function showCategory($category){
$data = DB::select('select articles.*,
tags.category
from articles
join tags on articles.id = tags.article_id
where lang = ? AND tags.category = ?', ['ru', $category]);
return $data;
}
In my Article and Tag models a have those relationships, respectively:
Article.php
public function tags(){
return $this->hasMany('\App\Tag');
}
Tag.php
public function articles(){
return $this->hasMany('App\Article');
}
This implementation works for me, but I am still curious how can I achieve the same result with Eloquent and eager load tag's category.