Reputation: 2387
In laravel 4 I want to get the current id of the band I'm making so I can store it with the festival id I'm attaching the band with. How can I get the current id in laravel 4 using eloquent?
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);
}
}
Upvotes: 1
Views: 119
Reputation: 24661
Save the band first...
$bands->save();
Then use the $bands
model to get the id for your $band_festivals
model:
$band_festivals->band_id = $bands->id;
$band_festivals->save();
Upvotes: 1