Laravel 4: many to many (insertion)

mwafi

I have these tables in DB:

[posts, cats (categories), posts_cats (pivote)]

the relation between posts table and cats is many to many

I declared the relation in the models classes:

//Post.php
public function cats()
{
    return $this->belongsToMany('cats');
}



//Cats.php
public function post()
{
    return $this->belongsToMany('posts');
}

the question is, How to insert new post with multiple categories?

thanks,

Ilyes512

Let's say you know the id of the post then you can attach a single cat like this:

Post::find($post_id)->cats()->attach($cat_id);

Or attach multiple cats like this:

$cat_ids = array(1,2,3,4);
Post::find($post_id)->cats()->attach($cat_ids);

If you got the Post model object in a variable, lets say $post:

$post->cats()->attach($cat_id);

// Or with multiple
$cat_ids = array(1,2,3,4);
$post->cats()->attach($cat_ids);

If you have a single category as an model object in, lets say $model:

$post->cats()->save($model);

Watch out with @Gadoma's answer. Its not wrong, but if you want to add categories to an post that already has categories then you should use attach() instead of sync(). Sync() will delete all others that are not provided to it when used.

edit:
So if you are creating a new Post then you probably are doing something like this:

$post = new Post;
$post->title = 'The title';
$post->something_else = 'Lorem';
$post->save();

//So now you have both the model object (the $post variable) and the id ($post->id).

$post->cats()->attach($cat_ids);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel 4: many to many (insertion)

From Dev

Laravel 4 many to many relationships

From Dev

Laravel 4 whereIn and many to many

From Dev

Laravel One-to-Many relation - Insertion

From Dev

Laravel 4 Many to Many with Eloquent and checkboxes

From Dev

Laravel 4 - Chaining Many-to-Many relationships

From Dev

Eager loading with many to many relationship in laravel 4

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

Laravel 4 : How to get selected/specific columns in a many to many relationship?

From Dev

Remove reationship from many-to-many models in laravel 4

From Dev

Laravel 4 : Authentication when having a many to many relationship

From Dev

In Laravel 4 Using creating a many to many relationship between 2 databases

From Dev

Laravel 4 many to many relationship error when no records match

From Dev

laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

one to many relationship help in laravel 4

From Dev

Pass many optional parameters to route in Laravel 4

From Dev

Laravel 4 Encryption: how many characters to expect

From Dev

Too Many Connections w/ Laravel 4 and MySQL

From Dev

Laravel 4 Seeding many rows, artisan stops

From Dev

Laravel 4 Seeding many rows, artisan stops

From Dev

Laravel 4 not returning one-to-many

From Dev

Laravel 4 Encryption: how many characters to expect

From Dev

Laravel eloquent many to many to many model

Related Related

HotTag

Archive