categories
テーブルを作成し、それをarticles
テーブルに関連付けました。
$table->foreign('category_id')->references('id')->on('categories');
また、「記事の作成」フォームに「category_id」という名前のカテゴリ選択フィールドを作成しました。新しい記事を作成しようとしているときに、送信ボタンを押した後、次のエラーが発生します。
Connection.php行725のQueryException:SQLSTATE [23000]:整合性制約違反:1452子行を追加または更新できません:外部キー制約が失敗します(
laratry
。articles
、CONSTRAINTarticles_category_id_foreign
FOREIGN KEY(category_id
)REFERENCEScategories
(id
))(SQL:insert intoarticles
(title
、body
、published_at
、user_id
、updated_at
、created_at
)の値(これは、試験物品である、これはエラーとは何かを見つけることを試みるためのさらに別の試験物品である。、2016年8月15日16時11分53秒、1、2016年8月15日午後4時11 :53、2016-08-15 16:11:53))
もしIますprint_r($要求- >すべての()) 、
Array (
[_token] => E4fWVTpf2T5y23uOnaA0EK1hHyi2hYvkJZa99V3G
[title] => This is a test article
[body] => This is yet another test article for trying to find what the error is.
[category_id] => 1
[published_at] => 2016-08-15
[tag_list] => Array ( [0] => 1 )
)
私のコントローラーの記事のストアメソッド:
class ArticlesController extends Controller
{
public function store(Request $request)
{
Auth::user()->articles()->create($request->all());
flash()->success('Your article has been created.');
return redirect('articles');
}
}
私のモデル:
class Article extends Model
{
protected $fillable = ['title','body','published_at','user_id'];
protected $dates = ['published_at','created_at'];
public function category()
{
return $this->belongsTo('App\Category');
}
}
-
class Category extends Model
{
protected $fillable = ['name','slug'];
protected $dates = ['created_at'];
public function articles()
{
return $this->hasMany('App\Article');
}
}
category_idがありません。
変化する
protected $fillable = ['title','body','published_at','user_id'];
に
protected $fillable = ['title','body','published_at','user_id', 'category_id'];
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加