how do you group a list of data?

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:

  1. 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');
}

  1. 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.

Leave a Comment

Your email address will not be published.

Scroll to Top