SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'crew_ids' for column `uj`.`crew_uj`.`crew_id` at row 1

126 views Asked by At

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 into crew_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>
1

There are 1 answers

6
linktoahref On

You can access the validated input as an array.

Hence this code would needs to be updated as:

$validatedData = $request->validated();
$uj = new UJ();
...
$uj->Crew()->attach($validatedData['crew_ids']);

Syncing Associations

You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table.

$uj->Crew()->sync($validatedData['crew_ids']);