2017-07-21 102 views
2

我想创建一个博客,我有两个模型,Post和Tag。我想用透视表连接他们两个。这是一种多对多的关系,我无法弄清楚如何将帖子和标签链接在一起。当我尝试这样做时,它不会在我的数据库上返回任何内容。帖子有标题和内容,而标签只有名称。 我读过了,我必须使用同步方法或attach-detach,但我不知道该在哪里做。它是在帖子路线或标签路线?我已经通过利用包括职位和将它们分组在routes.php文件标签的路线:2个模型与数据透视表,多对多的关系

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

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

这是我到目前为止有:

我的Post模型:

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

代码看起来不错。你在尝试什么不起作用? –

+0

当我想创建一篇文章时,我希望唯一的用户,即管理员,将他想要的任何标签与他的帖子关联起来。我应该使用sync方法创建什么路径来将帖子与标签连接起来,因为到目前为止,我的帖子和我的标签都是在创建时存储的,但是没有任何信息存储在post_tag表中 – livia

+0

@joel hinz我现在意识到我的crud看起来不错,但我有一个与你无关的问题。我可以编辑洞问题以问我如何在帖子的创建视图中实现标签复选框? – livia

回答

1

嘿,那里我在拉拉维尔也是新人,我也有同样的问题。在你的情况下,我认为最好的做法是附加在你的PostController.php。让我分享代码为你,我希望它能够帮助

PostController中

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

编辑:添加例如图

<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>

然后在PostC ontroller.php你可以得到的ID,如果用户选中复选框:

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

EDIT2:添加例如使用同步

在这里,我给你使用的同步小例子,首先让我们设置一个职位它有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

+0

你能告诉我一个例子使用同步() ? 这是事情。当管理员创建新帖子或对其进行编辑时,他可以为帖子分配一个或多个标签,无论他想要哪个标签。他也有权创建新的标签。如果创建的标签自动同步不是更好吗?这样他可以删除,创建新标签并从列表中选择一个或多个标签。 p.s.我仍然没有在我的帖子创建视图中创建标签的下拉列表 – livia

+0

啊看看我的编辑亲爱的:D –

+0

谢谢@Arigi :) – livia

1

使用sync()功能,例如:

$post->tags()->sync($array_of_tags_ids) 
+0

是否有必要只包括数字为ID?我不知道管理员将为他的帖子选择什么标签。有没有另一种方法,而不是使用标签ID创建数组? – livia

+0

@livia你有什么想法?因为要设置关系,只需使用透视表传递标记和帖子的ID –

+0

当管理员创建新帖子或对其进行编辑时,他可以为帖子分配一个或多个标签,无论他想要哪个标签。他也有权创建新的标签。从逻辑上讲,如果我通过tag_id 1或3或其他什么,我是不是强迫他只在1或3之间选择? – livia

相关问题