Laravel Many To Many Сохранить

У меня есть модели тегов и сообщений.

Тег Миграции

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

Миграционный Пост

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

AppTag

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


    protected $fillable = ['name'];

    /**
     * @return IlluminateDatabaseEloquentRelationsBelongsToMany
     * Many to Many relations make with post
     */
    public function posts()
    {
        return $this->belongsToMany('AppPost','post_tag','post_id');
    }
}

AppPost

class Post extends Model
{

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


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

    /**
     * @return IlluminateDatabaseEloquentRelationsBelongsTo
     * one to many relationship with user
     */
    public function user()
    {
        return $this->belongsTo('AppUser','user_id');
    }

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

    }


}

Я хочу прикрепить сообщения и теги.Есть создать почтовую форму и я хочу к этой форме прикрепить теги. Как я это сделаю?

PostController@store

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

Я нашел в документации Laravel, но я не использую метод

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

1 ответ