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

编程入门 行业动态 更新时间:2024-10-21 06:03:09
本文介绍了如何修复错误找不到基表或视图:1146表laravel关系表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是laravel的新手,我试图在表之间建立多对多关系,当我将数据插入数据库时​​出现问题

I am a new of laravel I try to create relationship many to many between table,My problem when I am insert data in to database I got errors

QueryException:SQLSTATE [42S02]:找不到基表或视图:1146表'learn.category_posts'不存在(SQL:插入 category_posts ( category_id , posts_id )值(4,))

QueryException in Connection.php line 713: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'learn.category_posts' doesn't exist (SQL: insert into category_posts (category_id, posts_id) values (4, ))

有人可以帮助我吗?这是我的迁移和代码:

can anyone help me pls . and here below is my migrate and code:

2016_08_04_131009_create_table_posts.php

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

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_create_table_category_posts.php

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(); }); }

和我的模型Posts.php

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

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

我的PostsController.php

My PostsController.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'); }

我的视图create.blade.php

My View 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()!!}

推荐答案

Laravel似乎正在尝试使用 category_posts 表(由于多对多关系).但是您没有此表,因为您已经创建了 category_post 表.将表的名称更改为 category_posts .

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.

更多推荐

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

本文发布于:2023-10-27 23:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534820.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   视图   错误   关系   laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!