for using laravel-eloquent
follow the below way:
at the first, set relation between post and tag in their models, of course, this related to your Database analysis:
- if your relationship is one to many:
// Post Model
public function tags()
{
return $this->hasMany('App\Models\Tag');
}
// Tag Model
public function posts()
{
return $this->hasMany('App\Models\Post');
}
- or your relationship is many to many:
// Post Model
public function tags()
{
return $this->belongsToMany('App\Models\Tag');
}
// Tag Model
public function posts()
{
return $this->belongsToMany('App\Models\Post');
}
at last, when you want to get posts with their self tags, you just enough do this:
Post::with('tags')->get();
try this way!
for more information read the docs.
CLICK HERE to find out more related problems solutions.