什麼是將stream_id作爲外鍵保存在垃圾表內的最佳方法 我已經創建了這兩個表的表。Laravel將外鍵保存在另一個表中
遷移:
public function up()
{
Schema::table('junk', function(Blueprint $table)
{
$table->integer('stream_id')->after('id')->unsigned();
});
}
控制器功能:
public function create(Request $request)
{
// create junk, junk shall contain the stream id as a foreign key (save in database)
$junk = new Junk();
// stream information data -> the data is saved correctly here
$data = $request->all();
$stream = new Stream();
$stream->fill($data);
if($stream->save())
{
return redirect()->route('stream.new')->with('success', 'saved.');
}
else
{
return redirect()->route('stream.new')->with('error', 'not saved.')->withInput();
}
}
我的垃圾型號:
public function junk()
{
return $this->belongsTo('Stream', 'junk_id');
}
我流模型
public function stream()
{
return $this->belongsTo('Junk', 'stream_id');
}
沒有工作我把我的模型添加到quesiton頭腦看看 – Olipol
如果關係是一對一,方法是'hasOne()' – wbail