I am trying to add a user_id
array key when doing an eloquent insert. I have the following setup:
View
{!! Form::label('supervisors', 'Assign Supervisor(s)') !!}
{!! Form::select('supervisors[][supervisor_id]', $supervisors, null, ['class' => 'chosen-select', 'multiple']) !!}
User Table
id first_name
12 John
UserSupervisors Table
id user_id supervisor_id
1 12 1
Currently the request $request->get('supervisors')
outputs this:
array:1 [▼
0 => array:1 [▼
"supervisor_id" => "1"
]
]
However, I would like it to output this:
array:1 [▼
0 => array:1 [▼
"supervisor_id" => "1",
"user_id" => "12"
]
]
How can I achieve this dynamically?
You can easily use this code to solve the problem:
$user = new User();
$user->user_id = 10;
$user->supervisor_id = Input::get('supervisor_id');
$user->save();
to add an item to the Request $request
, you can use merge()
like this :
$user_id = 4; //your user id
$request->merge([supervisors => ["user_id" => $user_id]]);
and $request->get('supervisors')
will give you the output you need
hope that helps