我是一个新手,我试图在表之间建立多对多的关系,我的问题是当我在数据库中插入数据时,我遇到了错误

查询连接中的异常.php第713行:SQLSTATE[42S02]:找不到基表或视图:1146 table'learn.类别_posts'不存在(SQL:插入category_posts(category_idposts_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_创建_表格_类别_帖子.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();
    });
}

and my model 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');
}

我的视图创建.刀身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()!!}

推荐答案

It seems Laravel is trying to use category_posts table (because of many-to-many relationship). But you don't have this table, because you've created category_post table. Change name of the table to category_posts.

Laravel相关问答推荐

如何使中继器项目计数动态Laravel Filament V3?

警告:构建Reaction App(VITE)后无法解码下载的字体警告

Laravel中的策略对我不起作用,这是我的代码

运行NPM Prod时出现VUE问题

如何解决此 Backblaze B2 S3 兼容 API 错误?

如何计算具有特定 ID Laravel 的行

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

laravel Eloquent 模型更新事件未触发

错误try 访问 null 类型值的数组偏移量laravel 5.8.26 Php 7.4.2

Laravel - 如何恢复本地存储符号链接并刷新

laravel 4 - 如何限制(采取和跳过)Eloquent的 ORM?

Laravel Eloquent 模型中的字段

判断输入是否来自控制台

如何从 Laravel 中的资源中获取图像?

Laravel 5.4 - Mix - 如何运行浏览器实时重新加载

[Vue 警告]:找不到元素:#app

Laravel 5 - 跳过迁移

Laravel 部署……有标准的方式吗?

在 Laravel 中翻译特定语言

如何从 laravel 5.1 中数据库表的 created_at 属性中 Select 年份和月份?