我是一个新的laravel我尝试在表之间创建多对多的关系,当我将数据插入数据库时出现问题我遇到了错误
Connection.php第713行中的QueryException:SQLSTATE [42S02]:未找到基表或视图:1146表'learn.category_posts'不存在(SQL:插入category_posts
(category_id
,posts_id
)值(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();
});
}
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
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
<?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
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
public function posts()
{
return $this->belongsToMany('App\Posts');
}
}
我的帖子控制器.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
{!!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
。
Laravel试图猜测表的名称,你必须直接指定它,这样它就不会给你那个错误。
试试这个:
class NameModel extends Model {
public $table = 'name_exact_of_the_table';
我希望有所帮助!
Schema::table
是修改现有表,使用Schema::create
创建新表。
最简单的方法是,更改为模型分配的默认表名。简单地下面的代码,protected $table = 'category_posts';
而不是protected $table = 'posts';
然后它会做的伎俩。
但是,如果您参考Laravel文档,您将找到答案。这就是它的意思,
按照惯例,“蛇案例”,类(模型)的复数名称将用作表名,除非明确指定其他名称
最好使用artisan命令同时创建模型和迁移文件,使用以下命令,
php artisan make:model Test --migration
这将在Laravel项目中创建模型类和迁移类。假设它创建了以下文件,
test.php的
2018_06_22_142912_create_tests_table.php
如果你看一下你会看到的那两个文件中的代码,
2018_06_22_142912_create_tests_table.php文件的up函数,
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
这里它自动生成表名为'tests'的代码,这是Test.php文件中该类的复数名称。
导致您的表无法迁移的主要问题是您在“AppServiceProvider.php”上运行查询尝试检查您的serviceprovider
并同时禁用代码,并运行php artisan migrate
你应该在你的PostController中更改/添加:(并将PostsController更改为PostController)
public function create()
{
$categories = Category::all();
return view('create',compact('categories'));
}
public function store(Request $request)
{
$post = new Posts;
$post->title = $request->get('title'); // CHANGE THIS
$post->body = $request->get('body'); // CHANGE THIS
$post->save(); // ADD THIS
$post->categories()->attach($request->get('categories_id')); // CHANGE THIS
return redirect()->route('posts.index'); // PS ON THIS ONE
}
PS:使用route()意味着您已经为此命名了路线
Route::get('example', 'ExampleController@getExample')->name('getExample');
UPDATE
以上评论也是正确的,将您的“帖子”模式更改为“发布”
你可以简单地修复在Post Model中添加它的问题,
public function categories()
{
return $this->belongsToMany('App\Category','category_post','post_id','category_id');
}
category_post
表示您要使用的表格。
post_id
表示要存储帖子ID的列。
category_id
指示要存储类别ID的列。
如果您在创建表之前使用表,请尝试检查应用程序,例如appServiceProvider.php
你可能在不创建它的情况下调用表,如果你是,请注释然后运行php artisan migrate。
为了解决你的基表或查看未找到错误你可以做的因为@Alexey Mezenin说更改表名称category_post
到category_posts
,
但如果你不想改变名称,就像在我的情况下我使用inventory
表所以我不想通过s
后缀它所以我将在模型中提供表名为protected $table = 'Table_name_as_you_want'
然后没有必要更改表名称:
更改您遇到错误的模块的模型,例如:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
protected $table = 'inventory';
protected $fillable = [
'supply', 'order',
];
}
你必须在模型中提供表名,然后它不会给出错误。