在 laravel 4 中,我想获取我正在制作的乐队的当前 ID,这样我就可以将它与我要附加乐队的节日 ID 一起存储。如何使用 Eloquent 在 Laravel 4 中获取当前 ID?
public function store()
{
$input = Input::all();
$rules = array(
'band_name' => 'required',
'band_members' => 'required',
'band_genre' => 'required',
'band_startdate' => 'required'
);
$validator = Validator::make($input, $rules);
if($validator->passes())
{
$bands = new Band();
$band_festivals = new BandFestival();
//here I want the current id of the band i'm making to store it in the band_festivals table
$band_festivals->band_id = Input::get('band_id');
$band_festivals->festival_id = Input::get('festival_id');
$bands->band_id = Input::get('band_id');
$bands->band_name = $input['band_name'];
$bands->band_members = $input['band_members'];
$bands->band_genre = $input['band_genre'];
$bands->band_startdate = $input['band_startdate'];
$bands->save();
$band_festivals->save();
Session::flash('message', 'Successfully created festival!');
return Redirect::to('bands');
}
else {
return Redirect::to('bands/create')->withInput()->withErrors($validator);
}
}
最佳答案
先保存乐队...
$bands->save();
然后使用 $bands
模型获取您的 $band_festivals
模型的 ID:
$band_festivals->band_id = $bands->id;
$band_festivals->save();
关于php - 拉维尔 4 : get current id to put id's from 2 tables in a third table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271544/