创建类别并重定向到category.index后,在laravel中找不到基表或视图

创建类别并重定向到category.index后,在laravel中找不到基表或视图,laravel,many-to-many,relationship,Laravel,Many To Many,Relationship,我在创建目录时遇到问题。 在我将类别关系更改为多对多之前,我没有问题 当我发布新类别,然后将其重定向到类别列表页面时,我收到以下错误: <?php namespace App\Models; use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\Model; class Category extends Model { use Sluggable; protected $

我在创建目录时遇到问题。 在我将类别关系更改为多对多之前,我没有问题

当我发布新类别,然后将其重定向到类别列表页面时,我收到以下错误:

<?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'
            ]
        ];
    }
}
SQLSTATE[42S02]:未找到基表或视图:1146表“myblog.category_post”不存在(SQL:选择
posts
*,
category\u post
类别id作为
pivot\u类别id
category\u post
作为
pivot\u post id
来自
posts
内部连接
category\u post
posts
上其中
category\u post
category\u id
=1)(视图:C:\Users\M0RT3Z4\Desktop\MyBlog\resources\views\admin\category\index.blade.php)

但是类别插入到数据库中。如果记录插入到类别表中,类别列表将不显示,并且我将得到错误

发布模型: 名称空间应用程序\模型

使用Cviebrock\EloquentSluggable\Sluggable; 使用Illumb\Database\Elount\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'
            ]
        ];
    }
}

您应该使用迁移命名约定的最佳实践来避免此问题

create_post_category
重命名为
category_post
(按字母顺序)


或者您可以在
$this->belongToMany(Category::class,'Category_post')
中指定透视表和键的其他参数

我已经更新了答案以包含透视表,请尝试它,如果它不起作用,请确保运行
php artisan migrate
<?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');
    }
}
<?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');
    }
}