我是一个新的laravel我尝试在表之间创建多对多的关系,当我将数据插入数据库时出现问题我遇到了错误
Connection.php第713行中的QueryException:SQLSTATE [42S02]:找不到基表或视图:1146表'learn.category_posts'不存在(SQL:insert into category_posts(category_id,posts_id)values(4,))
任何人都可以帮助我.以下是我的迁移和代码:
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();
});
}
Run Code Online (Sandbox Code Playgroud)
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();
});
}
Run Code Online (Sandbox Code Playgroud)
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();
});
}
Run Code Online (Sandbox Code Playgroud)
和我的模特Posts.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $table = 'posts';
public function categories()
{
return …Run Code Online (Sandbox Code Playgroud)