如何在laravel中插入多类别和多列不同类别的帖子?

use*_*709 2 laravel laravel-5 laravel-5.2

大家好,我是一个新的 Laravel,我的项目有问题。我有2张桌子:

posts: id title category_id
category: id name(PHP, Laravel, Ruby)
Run Code Online (Sandbox Code Playgroud)

例如:我想要这样的结果,如果我在帖子数据库中插入一个帖子 title=Jonh 并选择两个类别名称(PHP,Laravel),结果将是两列,如帖子(id=1 title(jonh) category_id(1) , id=2 title(jonh) category_id(2)) 但它仍然不起作用我尝试在 google 和 youtube 中找到它,但它不起作用,这是我的代码:

View(create.blade.php)
{!!Form::open(array('route' => 'store', 'method' => 'POST'))!!}

{{Form::text('title')}}<br>

@foreach($category as $categories)
{{Form::label('category',$categories->name)}}
{{Form::checkbox('category_id', $categories->id) }}
@endforeach
<br>
{{Form::submit('submit')}}
{!!Form::close()!!}

Controller(PostsController.php)
public function create()
    {
        $category = Category::all();
        return view('create',compact('category'));
    }
    public function store(Request $request)
    {
       $post = new Posts;
       $post->title = $request['title'];
       $post->category_id = $request['category_id'];
       $post->save();
    }
Run Code Online (Sandbox Code Playgroud)

请帮助

Luc*_*cas 6

我认为 Post 和 Category 之间的关系是不正确的。一个帖子属于零个或多个类别(您可以在应用层限制为一个或多个),一个类别可以与零个或多个帖子相关联。这种类型的关系称为多对多

帖子属于多类别
类别属于多多帖子

下面我们有表结构:

----- 帖子 -----
id INT
标题 VARCHAR(100)
正文文本

----- category_post -----
category_id INT
post_id INT

----- 类别 -----
id INT
名称 VARCHAR(100)

你应该做的是:

1 - 创建一个新的迁移到 category_post 表

<?php

// migrations/****_**_**_******_create_posts_table.php
Schema::create('posts', function(Blueprint $table) {
    $table->increments('id');
    $table->string('title', 100);
    $table->text('body');
});

// migrations/****_**_**_******_create_categories_table.php
Schema::create('categories', function(Blueprint $table) {
    $table->increments('id');
    $table->string('name', 100);
});

// migrations/****_**_**_******_create_category_post_table.php
Schema::create('category_post', function(Blueprint $table) {
    $table->integer('category_id')->unsigned();
    $table->integer('post_id')->unsigned();
    $table->primary(['category_id', 'post_id']);
    $table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
    $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});
Run Code Online (Sandbox Code Playgroud)

2 - 更改 Eloquent 模型中 Post 和 Category 之间的关系。

<?php

// Post.php
public function categories()
{
    return $this->belongsToMany(Category::class);
}

// Category.php
public function posts()
{
    return $this->belongsToMany(Post::class);
}
Run Code Online (Sandbox Code Playgroud)

现在你可以做这样的事情:

// PostController.php
public function create()
{
    $categories = Category::all();

    return view('posts.create', compact('categories'));
}

public function store(Request $request)
{
    $this->validate($request, [
        // Validate the max number of characters to avoid database truncation
        'title' => ['required', 'string', 'max:100'],
        'body' => ['required', 'string'],
        // The user should select at least one category
        'categories_id' => ['required', 'array', 'min:1'],
        'categories_id.*' => ['required', 'integer', 'exists:categories,id'],
    ]);

    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    $post->categories()->attach($request->categories_id);

    return redirect()->route('posts.index');
}

// views/posts/create.blade.php
<select name="categories_id" multiple>
@foreach ($categories as $category)
  <option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
Run Code Online (Sandbox Code Playgroud)