SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT

Viewed 5952

Hey I have problem when I insert my data from laravel to MySQL with error SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT

Here is store controller

public function store(Request $request)
{
    // $faker = Faker::create('id_ID');
    $image = $request->file('Product_photo');
    $extension = $image->getClientOriginalExtension();
    $name = $request->Name;
    $file_name = $name.'.'.$extension;
    $image->move("images/listcar/", $file_name);
    DB::table('car')
    ->insert([
        //'id' => $faker->unique()->numberBetween($min = 0, $max = 200),
        'product_photo' => $file_name,
        'name' => $request->Name,
        'production_year' => $request->Production_year,
        'price' => $request->Price,
        'merk' => $request->Merk,
        'description' => $request->Description,
        'varian' => $request->Variant,
        'machine' =>$request->Machine,
        'transmision' => $request->Transmision,
        'wheel' => $request->Wheel,
    ]);
    return redirect('/admin');

Here is my blade

<body>
<h1>Tambah Mobil</h1>
<div class="container">
    <form action="/car/add" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <th>Nama</th><br>
        <input type="text" name="Name" value=""><br/><br>
    <th>Tahun Produksi</th><br>
        <input type="text" name="Production_year" value=""><br/><br>
    <th>Harga</th><br>
        <input type="number" name="Price" value=""><br/><br>
    <th>Select Product Image</th><br>
        <input type="file" name="Product_photo" /><br/><br>
    <th>Merek</th><br>
        <input type="text" name="Merk" value=""><br/><br>
    <th>Deskripsi</th><br>
        <input type="textarea" name="Description" value=""><br/><br>
    <th>Varian</th><br>
        <input type="text" name="Variant" value=""><br/><br>
    <th>Mesin</th><br>
        <input type="text" name="Machine" value=""><br/><br>
    <th>Transmisi</th><br>
        <input type="text" name="Transmision" value=""><br/><br>
    <th>Penggerak Roda</th><br>
        <input type="text" name="Wheel" value=""><br/><br>
    <br>
        <input class="btn btn-primary" type="submit" value="Tambah Data Mobil">
    </form>
    <br>
    <br>
</div>

MySQL structure enter image description here

I really dont know where I went wrong, so please tell me if there is a wrong concepts

4 Answers

Change collation on mysql table - utf8mb4_general_ci : )

Check you model $fillable array. Ensure all columns that records will be inserted are part of your $fillable

i had same problem and solved:

if you save non-json into JSON field this error occurred like this:

    $item = new WhatsAppWebhook($data);
    $item->save();
    return $item;

you need to encode

    $payload = array("payload"=>json_encode($data));
    $item = new WhatsAppWebhook($payload);
    $item->save();
    return $item;
$user_role = array(('role')=>json_encode($request['role']));
$user_role = implode(', ', $user_role);
Related