lundi 26 février 2018

get extra data in Many To Many eloquent-relationships in laravel

I create a table like this:

posts

  id title

categories

 id name

category_post

   post_id
   category_id
   custom_value

in Category model:

public function posts()
{
        return $this->belongsToMany(Post::class);
}

with this command:

$category = Category::find(1);
$posts = $category->posts()->get();

I can get all of posts.

It is possible to return custom_value in category_post with each related post?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire