dongzhi5846 2017-10-07 20:08
浏览 19
已采纳

Laravel 5.4及以上:没有任何内容发布到数据透视表

I've scoured the Laravel docs and similar questions here on Stack but can't seem to find anything that works.

I have a pivot table set up called post_tag, which is meant to gather the post_id and the tag_id from the post table and tag table, respectively - but they don't. Data posts to the post table and to the tag table perfectly, but the post_tag table remains empty. Here is my code, I will be forever indebted to anyone who is able to provide an answer, or at least point me in the right direction, as I've tried everything I can find thus far.

Just to confirm, I've omitted chunks of code from each file if they're not relevant to the many-to-many relationship:

Post.php

class Post extends Model
{

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }

}

Tag.php

class Tag extends Model
{

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

}

PostController.php

    public function store(Request $request)
{
    $this->validate(request(), [

        'content' => 'required',
        'tag' => 'required'
    ]);

    Post::create([

        'content' => request('content'),
        'user_id' => auth()->id()
    ]);

    Tag::create([

        'tag' => request('tag'),
    ]);

    return redirect('/');

}

create_post_tag_table.php

class CreatePostTagTable extends Migration
{
public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {


        $table->integer('post_id');
        $table->integer('tag_id');
        $table->primary(['post_id', 'tag_id']);
    });
}

I've also tried explicitly setting up foreign keys in the database. These did indeed show up as foreign keys when I checked them in Sequel Pro, but my post_tag table was still showing up as empty whenever I made new posts.

I've also tried using tinker to manually attach IDs in the post_tag table which worked - I found out about this from the Laravel 5.4 fundamentals video series on Laracasts, but there was no mention of how to put this into my code so that it would dynamically add the post_id and the tag_id to the pivot table whenever I make a new post.

Thanks and look forward to your help!

  • 写回答

1条回答 默认 最新

  • dongmi4927 2017-10-07 20:15
    关注

    You just need to attach your models. It looks like:

    $post = Post::create([
    
        'content' => request('content'),
        'user_id' => auth()->id()
    ]);
    
    $tag = Tag::create([
    
        'tag' => request('tag'),
    ]);
    $post->tags()->attach($tag->id);
    

    Read more https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集