Please help, so i want to use foreach to loop and attach data to database using Eloquent in my Controller, i passed this is the parsed data as an array :
array:3 [▼
0 => "3"
1 => "7"
2 => "9"
]
and i want to loop (foreach) and save it to tables using attach() so it can be saved with the same ID in one time like this :
Course_ID | Facilitator_Id
--------------------------
1 | 2
1 | 3
1 | 4
--------------------------
this is the piece of blade :
<form method="POST" action="{{ '/courses' }}" enctype="multipart/form-data">
...
<div class="form-group row">
<label class="col-3" for="fasil_id">Fasilitator</label>
<div class="col-9">
<select class="form-control @error('fasil_id') is-invalid @enderror bootstrap-select" data-style="btn-white" multiple name="fasil_id[]" selected="{{ old('fasil_id') }}" required autocomplete="fasil_id">
@foreach($facilitators as $data)
<option value="{{$data->id}}">{{$data->nama}}</option>
@endforeach
</select>
@error('fasil_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
...
</form>
im using name="fasil_id[]" in the markup properties
and currently i'm using this method for looping :
foreach($request->fasil_id as $fasil_id) { Course::find($id)->facilitators()->attach([$fasil_id]); }
but, sadly i always get this error :
htmlspecialchars() expects parameter 1 to be string, array given
->attach([$fasil_id])use->attach($fasil_id).