如何修复错误未找到基表或视图:1146表laravel关系表?

He7*_*7nG 7 laravel laravel-5 laravel-5.2

我是一个新的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 $this->belongsToMany('App\Category');
    }
}
Run Code Online (Sandbox Code Playgroud)

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');
    }   
}
Run Code Online (Sandbox Code Playgroud)

我的帖子控制器.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');
}
Run Code Online (Sandbox Code Playgroud)

我的视图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()!!}
Run Code Online (Sandbox Code Playgroud)

小智 13

导致您的表无法迁移的主要问题是您在“AppServiceProvider.php”上运行查询尝试检查您的serviceprovider代码并同时禁用代码,然后运行php artisan migrate


Ale*_*nin 9

似乎Laravel试图使用category_posts表格(因为多对多的关系).但是你没有这个表,因为你已经创建了category_post表.将表的名称更改为category_posts.


Ana*_*ali 7

您可以在Post Model 中添加它,

public function categories()
{
 return $this->belongsToMany('App\Category','category_post','post_id','category_id');                 
}
Run Code Online (Sandbox Code Playgroud)

category_post指明您要使用的表。
post_id指示要存储帖子 ID 的列。
category_id指示要存储类别 ID 的列。


Har*_*hil 7

要解决您的基表或视图未找到错误,您可以执行@Alexey Mezenin 所说的操作,将表名称更改category_postcategory_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',
    ];
}
Run Code Online (Sandbox Code Playgroud)

您必须在模型中提供表名称,这样就不会给出错误。


小智 6

Laravel尝试猜测表的名称,您必须直接指定它,以免出现该错误。

尝试这个:

class NameModel extends Model {
    public $table = 'name_exact_of_the_table';
Run Code Online (Sandbox Code Playgroud)

希望对您有所帮助!


Sha*_*hal 6

Schema::table是修改现有表,用于Schema::create创建新表。