1
votes

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

2
You don't set the primary key manually, the database will assign it when the record is inserted. Remove this 'id' => $faker->unique()->numberBetween($min = 0, $max = 200),Brian Lee
@vandettadyckies You need to set id as auto increment and then no need of setting it through Faker.Ankit Jindal
Reporting about error provide complete error message, do not strip it.Akina
okaiy, i allready update it, now i'm using auto increment, but same error still happeningvandettadyckies
Try clearing the table and start from scratch since your column "id" are messed up. It will be all god now since you added auto increment propertyPrasanth M P

2 Answers

0
votes

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

0
votes

Change collation on mysql table - utf8mb4_general_ci : )