私は私は私がエラーエラーを修正する方法ベーステーブルまたはビューが見つかりません:1146テーブルラーベル関係テーブル? SQLSTATE [42S02]:
Connection.phpライン713でQueryExceptionを得たデータベースにデータを挿入していたときに、テーブルの間に多くの多くの私の問題を関係を作成しようlaravelの新しい午前ベーステーブルまたはビューが見つかりません:誰もがplsは私を助けることができる
:(category_posts
(category_id
、posts_id
)の値(4)に挿入するSQLを)1146テーブル 'learn.category_posts' が存在しません。そして、ここで以下の私の移行とコードです:
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_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
<?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');
}
}
マイ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
{!!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()!!}
'learn'テーブルを作成しましたか? mysqlはそれを探していて見つけられません。 –
私はそれを作成しました – He7nG