2016-05-02 40 views
0

私はタグとポストのモデルを持っています。Laravel Many To Many Save

移行タグ

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

のApp \タグ

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 \ポスト

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(); 

    } 


} 

私は記事を添付したいとtags.Thereは、投稿フォームを作成し、このフォームは、タグを付けるに私が欲しいです。どうすればいいですか?

のPostControllerの@ストア

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

私はLaravelのドキュメントで見つかったが、私は方法

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

でコンビニについてはの同期のための検索これは少し混乱して、あなたはさらに明確だろうか? 'Post'モデルと' Tag'モデルを表示しますが、 'Post'を' User'に付けて 'User'に' Role'を付けるように見えます。あなたが何を正確に求めているのかは不明です。 – user3158900

+0

あなたはそうです。この質問を編集する – helloworld

+0

私は編集しました。私はポストとタグを添付したい – helloworld

答えて