简体   繁体   English

Laravel5.4中的多对多关系

[英]Many to Many relation in Laravel5.4

I Want to Create multi category for posts in database but I Just create one category with this code: post.php 我想为数据库中的帖子创建多个类别,但是我只使用以下代码创建一个类别:post.php

 public function Category()
{
    return $this->belongsTo('App\Category');
}

Category.php Category.php

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

posts_table: posts_table:

 public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('category_id');
        $table->string('title');
        $table->integer('price');
        $table->string('description');
        $table->timestamps();
    });
}

and view for create category is here: 并在此处查看创建类别的视图:

   <form method="post" action="/storePost">
        {{csrf_field()}}
         <input type="hidden" id="user_id" name="user_id" value="
              {{Auth::user()->id}}">

      <lable>title</lable>
      <input type="text" id="title" name="title">

      <label>description</label>
      <input type="text" id="description" name="description">


      <label>price</label>
      <input type="text" name="price" id="price">

      <label>Category</label>
      <select name="category_id">
          @foreach($categories as $category)
          <option value={{$category->id}}>{{$category->name}}</option>
              @endforeach

      </select>


      <button type="submit" id="AddProduct">add</button>

  </form>

And my postcontroller to create category is: 我创建类别的后控制器是:

  public function store()

  {

   Post::create([
       'user_id'=>request('user_id'),
       'title' => request('title'),
       'category_id'=>request('category_id'),
       'description'=>request('description'),
       'price'=>request('price'),
   ]);
   return redirect('/show');

} }

How I Can create multi category for one posts in table? 如何为表格中的一个帖子创建多类别?

You are going to need to design your database somewhat differently. 您将需要对数据库进行一些不同的设计。 You need a proper join table between the two tables. 您需要在两个表之间使用适当的联接表。 Your database should look something like this: 您的数据库应如下所示:

posts
  id
  //other

categories
  id
  //other

post_categories
  post_id
  category_id

Once you have the database setup with a proper join. 一旦使用正确的联接进行数据库设置。 You have to define the relations a little bit differently: 您必须对关系进行一些不同的定义:

// App\Models\Post
public function categories() {
    return $this->belongsToMany('App\Category', 'post_categories', 'category_id', 'post_id');
}

// App\Models\Category
public function posts() {
    return $this->belongsToMany('App\Post', 'post_categories', 'post_id', 'category_id'); 
}

You can then use attach and detach to add and remove relations: 然后,您可以使用attachdetach来添加和删除关系:

$post = Post::find(1);
$post->categories()->attach($categoryId);

You can read more about many-to-many relationships in the Laravel Documentation . 您可以在Laravel文档中阅读有关多对多关系的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM