创建类别并重定向到 category.index 后,在 laravel 中找不到基表或视图
Posted
技术标签:
【中文标题】创建类别并重定向到 category.index 后,在 laravel 中找不到基表或视图【英文标题】:base table or view not found in laravel after create category and redirect to category.index 【发布时间】:2018-12-01 04:30:21 【问题描述】:我在创建类别时遇到问题。 在我将类别关系更改为多对多之前我没有问题。
当我发布新类别并重定向到类别列表页面后,我收到此错误:
SQLSTATE[42S02]:未找到基表或视图:1146 表“myblog.category_post”不存在(SQL:选择posts
.*、category_post
.category_id
作为pivot_category_id
、@987654325 @.post_id
as pivot_post_id
from posts
inner join category_post
on posts
.id
= category_post
.post_id
where category_post
.category_id
= 1(查看) \Users\M0RT3Z4\Desktop\MyBlog\resources\views\admin\category\index.blade.php)
但类别插入到数据库中。如果记录插入到类别表中,类别列表将不会显示,我会得到错误。
后模型:
命名空间 App\Models;
使用 Cviebrock\EloquentSluggable\Sluggable; 使用 Illuminate\Database\Eloquent\Model;
class Post extends Model
use Sluggable;
protected $fillable = [
'title', 'body', 'views', 'category_id', 'user_id'
];
public function comment()
$this->hasMany(Comment::class);
public function category()
return $this->belongsToMany(Category::class);
public function tags()
return $this->belongsToMany(Tag::class);
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable(): array
return [
'slug' => [
'source' => 'title'
]
];
public function hastags($id)
return in_array($id, $this->tags()->pluck('id')->toArray());
public function hascategories($id)
return in_array($id,$this->category()->pluck('id')->toArray());
类别模型:
<?php
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
use Sluggable;
protected $fillable = [
'name','slug'
];
public function post()
return $this->belongsToMany(Post::class);
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable(): array
return [
'slug' => [
'source' => 'name'
]
];
类别控制器:
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
class CategoryController extends Controller
public function index()
$post = Post::all();
$cats = Category::orderBy('id','DESC')->paginate(10);
return view('admin.category.index',compact(['cats','post']));
public function create()
return view('admin.category.create');
public function show(Category $category)
return view('admin.category.show',compact('category'));
public function store(Request $request)
$this->validate($request, [
'name' => 'required|min:3|unique:categories',
]);
$cat = new Category();
$cat->name = $request->name;
$cat->slug = $request->slug;
$cat->save();
Session::flash('success','Category Created Successfully');
return redirect()->route('admin.category.index');
public function edit(Category $category)
return view('admin.category.edit',compact('category'));
public function update(Category $category,Request $request)
$this->validate($request, [
'name' => 'required|min:3|unique:categories',
]);
$category->name = $request->name;
$category->save();
Session::flash('update','Category Updated Successfully');
return redirect()->route('admin.category.index');
public function destroy(Category $category)
$category->delete();
Session::flash('delete','Category Deleted Successfully');
return redirect()->route('admin.category.index');
类别迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('categories', function (Blueprint $table)
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
);
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::dropIfExists('categories');
post_category 迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostCategory extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('post_category', function (Blueprint $table)
$table->unsignedInteger('post_id');
$table->unsignedInteger('category_id');
$table->foreign('post_id')->on('posts')->references('id')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('category_id')->on('categories')->references('id')->onUpdate('cascade')->onDelete('cascade');
$table->unique(['post_id', 'category_id']);
);
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::dropIfExists('post_category');
发布迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('posts', function (Blueprint $table)
$table->increments('id');
$table->string('title')->unique();
$table->string('slug')->unique()->nullable();
$table->text('body');
$table->string('image')->nullable();
$table->unsignedInteger('views')->default(0);
$table->unsignedInteger('category_id')->nullable();
$table->timestamps();
);
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::dropIfExists('posts');
【问题讨论】:
【参考方案1】:您应该使用迁移命名约定的最佳实践来避免此问题
将create_post_category
重命名为category_post
(按字母顺序)
或者您可以在 $this->belongsToMany(Category::class,'category_post')
中为数据透视表和键指定其他参数
【讨论】:
我已经更新了答案以包含数据透视表,试试吧,如果它不起作用,那么请确保您运行php artisan migrate
以上是关于创建类别并重定向到 category.index 后,在 laravel 中找不到基表或视图的主要内容,如果未能解决你的问题,请参考以下文章