kev
kev

Reputation: 11

Laravel Many To Many Save

I have tag and post models.

Migration Tag

class CreateTagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
        Schema::create('post_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id')->unsigned();
            $table->integer('tag_id')->unsigned();

            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
            $table->timestamp();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tags');
        schema::drop('post_tag');
    }
}

Migration Post

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('body');
            $table->text('filename');
            $table->timestamps();

    });
}

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::drop('posts');
    }
}

App\Tag

class Tag extends Model
{
    protected $table="tags";
    public $timestamps = true;


    protected $fillable = ['name'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     * Many to Many relations make with post
     */
    public function posts()
    {
        return $this->belongsToMany('App\Post','post_tag','post_id');
    }
}

App\Post

class Post extends Model
{

    protected $table="posts";
    public $timestamps = true;


    protected $fillable = ['title', 'body', 'filename'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * one to many relationship with user
     */
    public function user()
    {
        return $this->belongsTo('App\User','user_id');
    }

    /**
     * @return BelongsToMany relationship with tags
     * 
     */
    public function tags()
    {
        return $this->belongsToMany('App\Tag','post_tag','tag_id')->withTimestamps();

    }


}

I want to attach posts and tags.There is create post form and ı want to this form attach tags. How will do I?

PostController@store

public function store(Request $request)
{
    \Auth::user()->posts()->save(new Post($request->all()));
    return \Redirect::route('posts.index');
}

I found in Laravel Documentation but i dont use method

App\User::find(1)->roles()->save($role, ['expires' => $expires]);

Upvotes: 1

Views: 1378

Answers (1)

Ricardo Vigatti
Ricardo Vigatti

Reputation: 525

Use the sync method:

App\User::find(1)->roles()->sync($roles);

When $roles is an array of roles ids:

$roles = [
    0 => '1', // role_id 1
    1 => '2', // role_id 2
    2 => '5', // role_id 5
];

Search for Syncing For Convenience at the docs https://laravel.com/docs/5.2/eloquent-relationships#inserting-many-to-many-relationships

Upvotes: 1

Related Questions