Ich bin ein neues von Laravel Ich versuche Beziehung zwischen Tisch, mein Problem zu viele, viele zu schaffen, wenn ich einfügen Daten in Datenbank bin habe ich FehlerFehler beheben Basistabelle oder View nicht gefunden: 1146 Tabelle Laravel Beziehungstabelle?
QueryException in Connection.php Linie 713: SQLSTATE [42S02]: Basis Tabelle oder Sicht nicht gefunden: 1146 Table 'learn.category_posts' existiert nicht (SQL: insert into category_posts
(category_id
, posts_id
) Werte (4,))
kann mir jemand helfen, pls. und hier unten ist mein einwandern und Code:
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();
});
}
und mein Modell 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');
}
}
Mein 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');
}
Meine Ansicht 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()!!}
Haben Sie eine Lerntabelle erstellt? mysql sucht danach und kann es nicht finden. –
Ich hatte es erstellen – He7nG