Laravel Multiple Choices Input Error:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: ‘crew_ids’ for column
uj
.crew_uj
.crew_id
at row 1
insert intocrew_uj
(crew_id
,uj_id
) values (crew_ids, 15)
UJController :
public function store(Request $request)
{
// dd($request->all());
$validatedData = $request->validate([
'nama_event' => 'required',
'venue' => 'required',
'tanggal_show' => 'required|date',
'fee_pic' => 'nullable|numeric',
'fee_operator' => 'nullable|numeric',
'fee_transport' => 'nullable|numeric',
'notes' => 'nullable|string',
'crew_ids' => 'nullable|array',
'crew_ids.*' => 'exists:crew,crew_id|array',
]);
$uj = new UJ();
$uj->nama_event = $validatedData['nama_event'];
$uj->venue = $validatedData['venue'];
$uj->tanggal_show = $validatedData['tanggal_show'];
$uj->fee_pic = $validatedData['fee_pic'];
$uj->fee_operator = $validatedData['fee_operator'];
$uj->fee_transport = $validatedData['fee_transport'];
$uj->notes = $validatedData['notes'];
$totalUangJalan = $uj->fee_pic + $uj->fee_operator + $uj->fee_transport;
$uj->total_uang_jalan = $totalUangJalan;
$uj->save();
$uj->Crew()->attach('crew_ids');
return redirect()
->route('uj.data')
->with('success', 'Data UJ berhasil ditambahkan.');
}
UJ Model :
use HasFactory;
protected $table = 'uj';
protected $primaryKey = 'uj_id';
protected $fillable = [
'nama_event',
'venue',
'tanggal_show',
'fee_pic',
'fee_operator',
'fee_transport',
'notes',
'crew_ids'
];
public function Crew()
{
return $this->belongsToMany(Crew::class, 'crew_uj', 'uj_id', 'crew_id');
}
Crew Model :
use HasFactory;
protected $table = 'crew';
protected $primaryKey = 'crew_id';
protected $fillable = [
'nama_crew',
'divisi_crew',
'nominal_fee'
];
public function UJ()
{
return $this->belongsToMany(UJ::class, 'crew_uj', 'crew_id', 'uj_id');
}
View create.blade.php :
<div class="form-group">
<label for="crew_ids">Pilih Crew</label>
<select class="choices form-select multiple-remove" multiple="multiple" name="crew_ids[]" id="crew_ids">
<optgroup label="Pilih Crew">
@foreach($crewList as $crew)
<option value="{{ $crew->crew_id }}">{{ $crew->nama_crew }} - {{ $crew->divisi_crew }}</option>
@endforeach
</optgroup>
</select>
</div>
2
Answers
You can access the validated input as an array.
Hence this code would needs to be updated as:
Syncing Associations
The error message you are encountering indicates that there is an issue with the
crew_ids
value being passed when trying to insert data into thecrew_uj
table. It seems that the value'crew_ids'
is being treated as a string instead of an array of crew IDs.To resolve this issue, you need to modify the following line in your
store
method of theUJController
:Replace it with:
This change ensures that you are attaching the correct crew IDs from the
$validatedData
array.Additionally, there is a small mistake in the
UJ
model. Remove the'crew_ids'
from the$fillable
array because it is not a direct attribute of theUJ
model. Thecrew_ids
relationship is handled through theCrew
relationship.After making these changes, the code should look like this:
Make sure to update these changes in your code and try again.