我是laravel的新手,我试图在表之间建立多对多关系,当我将数据插入数据库时出现问题
I am a new of laravel I try to create relationship many to many between table,My problem when I am insert data in to database I got errors
QueryException:SQLSTATE [42S02]:找不到基表或视图:1146表'learn.category_posts'不存在(SQL:插入 category_posts ( category_id , posts_id )值(4,))
QueryException in Connection.php line 713: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'learn.category_posts' doesn't exist (SQL: insert into category_posts (category_id, posts_id) values (4, ))
有人可以帮助我吗?这是我的迁移和代码:
can anyone help me pls . and here below is my migrate and code:
2016_08_04_131009_create_table_posts.php
2016_08_04_131009_create_table_posts.php
public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->text('title'); $table->text('body'); $table->timestamps(); }); }2016_08_04_131053_create_table_categories.php
2016_08_04_131053_create_table_categories.php
public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); }2016_08_04_131413_create_table_category_posts.php
2016_08_04_131413_create_table_category_posts.php
public function up() { Schema::create('category_post', function (Blueprint $table) { $table->increments('id'); $table->integer('category_id')->unsigned(); $table->integer('post_id')->unsigned(); $table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade'); $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade'); $table->timestamps(); }); }和我的模型Posts.php
and my model Posts.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Posts extends Model { protected $table = 'posts'; public function categories() { return $this->belongsToMany('App\Category'); } }Category.php
Category.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Category extends Model { protected $table = 'categories'; public function posts() { return $this->belongsToMany('App\Posts'); } }我的PostsController.php
My PostsController.php
public function create() { $categories = Category::all(); return view('create',compact('categories')); } public function store(Request $request) { $post = new Posts; $post->title = $request->title; $post->body = $request->body; $post->categories()->attach($request->categories_id); return redirect()->route('posts.index'); }我的视图create.blade.php
My View create.blade.php
{!!Form::open(array('route' => 'store', 'method' => 'POST'))!!} {{Form::text('title')}}<br> {{Form::textarea('body')}}<br> <select name="categories_id" multiple> @foreach ($categories as $category) <option value="{{ $category->id }}">{{ $category->name }}</option> @endforeach </select> <br> {{Form::submit('submit')}} {!!Form::close()!!} 推荐答案Laravel似乎正在尝试使用 category_posts 表(由于多对多关系).但是您没有此表,因为您已经创建了 category_post 表.将表的名称更改为 category_posts .
It seems Laravel is trying to use category_posts table (because of many-to-many relationship). But you don't have this table, because you've created category_post table. Change name of the table to category_posts.
更多推荐
如何修复错误找不到基表或视图:1146表laravel关系表?
发布评论