我有创建cateogories的问题 . 在我将类别关系更改为多对多之前,我没有遇到任何问题 .
whene i post new category and after that its redirect to category list page, i got this error:
SQLSTATE [42S02]:找不到基表或视图:1146表'myblog.category_post'不存在(SQL:选择 posts
. *, category_post
. category_id
as pivot_category_id
, category_post
. 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)(查看:C:\ Users \ M0RT3Z4 \ Desktop \ MyBlog \ resources \ views \ admin \ category \ index.blade.php)
但是数据库中的类别插入 . 如果记录插入到类别表中,则类别列表将不会显示,我将收到错误 .
Post Model:
命名空间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());
}
}
Category Model:
<?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'
]
];
}
}
CategoryController:
<?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')
中为数据透视表和键指定其他参数