I have a form where a user can select multiple tags from a select box. I then need to create the shipment, and after I do that, I need to get all the ID's the user selected from the tags and update another table where the ID matches the tag IDs the user selected.
Here is the select box:
<select name="bins[]" class="form-control" id="shipping_bin_labels_select" multiple="multiple">
@foreach(\App\Bin::all() as $bin)
<option value="{{ $bin->id }}" {{ old('bins') }}>{{ $bin->label }}</option>
@endforeach
</select>
Here is how it looks:
Then I create the shipment:
public function store(Request $request)
{
$this->validate($request, [
'truck' => 'required',
'bins' => 'required',
'stand_id' => 'required',
]);
$standName = request('stand_id');
$standName = Stand::where('name', '=', $standName)->first();
Shipment::create([
'truck' => request('truck'),
'stand_id' => $standName->id
])->save();
...........
After that, I need to loop through all the bin tags, get their IDs and update the 'bins' table with the shipment ID I just made. I don't know how to get all the bins from the bins table with the bin IDs I just selected.
This is what I tried so far:
$bins = $request->input('bins');
$bins = implode(',', $bins);
// Tried this, but get empty collection. [$bins] (brackets) dont help either.
$bins = Bin::whereIn('id', $bins)->get();
// Tried this, but get "Call to a member function all() on string"
$bins = Bin::whereIn('id', $bins->all())->get();
// This is what I sort of need
$bins = Bin::whereIn('id', [3, 5, 6])->get();
For reference, when I DD this:
$bins = $request->input('bins');
$bins = implode(',', $bins);
dd($bins);
I get this:
Those are my bin IDs from the select box. Don't worry about the long ID string, thats how my ID's are formatted and stored in the bins table.