Reputation: 589
I am developing a booking form that has data that will fill two tables, users and booking table. In the controller I am getting the input fields and inserting the user fields first, then I do the insert for the booking but I need the id of the user I've just inserted, I have tried many methods without success, this is what I have done in my controller:
$User = new User;
User::create(array(
'lastname'=>Input::get('lastname'),
'name'=>Input::get('name'),
'address'=>Input::get('address'),
'cell_phone'=>Input::get('cell_phone'),
'email'=>Input::get('email')
));
// I try to get the inserted user id here
$userInserted=$User->id;
// And here I insert the booking with the user_id
$Booking = new Booking;
Booking::create(array(
'apartment_id'=>Input::get('apartment_id'),
'user_id'=>$userInserted,
'date_ini'=>Input::get('date_from'),
'date_fin'=>Input::get('date_to'),
'stay_cost'=>Input::get('stay_cost'),
'stay_days'=>Input::get('stay_days')
));
The problem is I am not getting the user Id. Any idea way?
thanks in advance
Upvotes: 0
Views: 158
Reputation: 83
You are assignin it wrong.
$newUser = User::create(...);
$insertedUserId = $newUser->id;
Your $User = new User;
makes no sense, because it just initializes emty User Object, but User::create
sends query to DB to store data and returns inserted Object.
Upvotes: 1