laravel - 2 个带有数据透视表的模型,多对多关系

标签 laravel many-to-many pivot

我正在尝试创建一个博客,我有两个模型:Post 和 Tag。我想用数据透视表将它们连接起来。这是多对多的关系,我不知道如何将帖子和标签链接在一起。当我尝试这样做时,它不会在我的数据库上返回任何内容。帖子有标题和内容,而标签只有名称。 我读过我必须使用同步方法或附加分离,但我不知道在哪里执行此操作。是在帖子路线还是标签路线上?我已将帖子和标签的路由包含在 paths.php 中,并使用以下方法对它们进行分组:

Route::resource('/tags', 'TagController');

Route::resource('/posts', 'PostController');

这是我到目前为止所拥有的:

我的帖子模型:

class Post extends Model{
protected $fillable = [
    'title', 'content'
];

protected $hidden = [
    'view_count'
];

public function tags()
{
    return $this->belongsToMany('App\Tag', 'post_tag');
}}

这是我的标签模型:

class Tag extends Model{
protected $fillable = [
    'name'
];

public function posts(){
    return $this->belongsToMany('App\Post', 'post_tag');
}}

这是我的 post_tag 数据透视表:

class CreatePostTagTable extends Migration{

public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned()->nullable()->index();
        $table->foreign('post_id')->references('id')->on('posts');
        $table->integer('tag_id')->unsigned()->nullable()->index();
        $table->foreign('tag_id')->references('id')->on('tags');
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('post_tag');
}}

最佳答案

嘿,我也是 Laravel 的新手,我和你有同样的问题。对于您的情况,我认为最佳实践是将其附加到您的 PostController.php 中。让我把代码分享给你,希望对你有帮助

后 Controller

public function store (Request $request) {

    // First we need to create variable that contain your tag_id
    // $tags = [1, 2, 4]; something like this
    $tags = $request->input('tags');



    // Now storing the data in posts table
    $post =  new Post;
    $post->title = $request->input('title');
    $post->content = $request->input('content');
    $post->save();

    //After save the post, we need to attach it
    $post->tags()->attach($tags);
}

编辑:添加示例 View

<div>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="1">
    <label for="subscribeNews">Tag 1</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="2">
    <label for="subscribeNews">Tag 2</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="3">
    <label for="subscribeNews">Tag 3</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="n">
    <label for="subscribeNews">More tag</label>
  </div>

然后在PostController.php中,如果用户选中复选框,您可以获得id:

$tags = $request->input('tags');

编辑2:添加使用同步的示例

在这里,我给你举一个使用同步的小例子,首先让我们设置一个有 5 个标签的帖子。最后我们只想设置它3

$post = Post::find(1) //get the post with id 1, this post have 5 tags
// Let's say that this post have 5 tags with this ids [1, 2, 3, 4, 5]

// And then admin edit this post and select the tag with id [1, 2, 6] we set it in $tags variable
$tags = $request->input('tags'); //[1, 2, 6]

// And the last we just need to use sync method like this
$post->tags()->sync($tags);

// After use that, it will detach the tag based from removed ids [3, 4, 5] and attach new tag if there's new tag id [6]
// And in the end the post just have 3 tags

好吧,这就是示例逻辑,我也在学习它,但我希望它可以帮助你:D

关于laravel - 2 个带有数据透视表的模型,多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45234705/

相关文章:

php - 拉拉维尔 5.3。生产环境如何配置?

php - Laravel 白屏 - 可能的 Mcrypt 问题?

mysql - 从一个表更新另一表中的值

具有时间戳排序的 MySQL 数据透视表查询

postgresql - 使用 PostgreSQL 9.3 的动态数据透视查询

java - Apache POI - 有没有办法创建源表是 SXSSFSheet 的数据透视表?

php - 如何知道更新了哪些字段

many-to-many - 如何让 EF 4.1 RC 代码优先使用正确的外键?

Grails 多对多关联和防止级联

laravel - Laravel 5 中的过滤器