什么是将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