laravellaravel-11

laravel 11 cannot save and upload the image


i am a beginner of laravel.i am creating a simple form along with image uploading.when i fill the form and select the image and upload image not uploading and data not saved in the databases.what i tried so far i attached below please check the code and get the solution. here is the code of form

 <form method="POST" action="{{ route('car.store') }}" enctype="multipart/form-data">

                @csrf
                        <div class="mb-3">
                            <label>Car title</label>
                            <input type="text"  class="form-control" name="title" id="exampleFormControlInput1" placeholder="Car title">
                        </div>

                        <div class="mb-3">
                            <label>Description</label>
                            <input type="text"  class="form-control" name="description" id="exampleFormControlInput1" placeholder="Description">
                        </div>

                        <div class="mb-3">
                        <label>Model</label>
                        <select name="model_id" id="model_id" class="form-control">
                            @foreach($carmodels as $id => $name)
                                <option value="{{ $id }}">
                                 {{ $name }}
                                </option>
                            @endforeach
                        </select>



                        </div>

                        <div class="mb-3">
                        <label>Member</label>
                        <select name="member_id" id="member_id" class="form-control">

                            @foreach($members as $id => $name)
                                <option value="{{ $id }}">
                                 {{ $name }}
                                </option>
                            @endforeach
                        </select>
                        </div>


                        <div class="mb-3">
                        <label>Fuel Type</label>
                        <select class="form-select" aria-label="Default select example">
                        <option selected>Open this select</option>
                        <option value="1">Diesel</option>
                        <option value="2">Patrol</option>
                        </select>
                        </div>


                        <div class="mb-3">
                            <label>Year</label>
                            <input type="text"  class="form-control" name="year" id="exampleFormControlInput1" placeholder="year">
                        </div>

                        <div class="mb-3">
                            <label>Mileage</label>
                            <input type="text"  class="form-control" name="mileage" id="exampleFormControlInput1" placeholder="Mileage">
                        </div>



                        <div class="mb-3">
                            <label>Price</label>
                            <input type="number"  class="form-control" name="price" id="exampleFormControlInput1" placeholder="Price">
                        </div>

                        <div class="mb-3">
                            <label>Photo</label>
                            <input type="file"  class="form-control" name="photo" id="exampleFormControlInput1" placeholder="Photo">
                        </div>

                
                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-primary me-2">Register</button>
                            <button type="reset" class="btn btn-warning">Reset</button>
                        </div>
                    </form>

store function

 public function store(Request $request)
{


    // Validate the incoming request data
    $validatedData = $request->validate([
        'title' => 'required',
        'description' => 'required',
        'model_id' => 'required',
        'member_id' => 'required',
        'fuel_type' => 'required',
        'year' => 'required',
        'mileage' => 'required',
        'price' => 'required',
        'photo' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust file size and allowed extensions as needed


    ]);

    // Check if the file was uploaded successfully
    if ($request->hasFile('photo'))
     {
        $fileName = $request->file('photo')->store('images', 'public');

        // Save the product with the file name
        $validatedData['photo'] = $fileName;




    }
    // Create a new Car record in the database using the validated data
    Car::create($validatedData);
    // Redirect the user back to the previous page
    return redirect()->back();
}
}

routes

Route::post('storecar', [CarController::class, 'store'])->name('car.store');
Route::get('allcar', [CarController::class, 'index']);

Solution

  • You're missing the name attribute on the fuel type select input. Without this, Laravel won't be able to capture the value.

    Update Fuel Type Select:

    <div class="mb-3">
      <label>Fuel Type</label>
      <select class="form-select" name="fuel_type" aria-label="Default select example">
        <option selected>Open this select</option>
        <option value="1">Diesel</option>
        <option value="2">Petrol</option>
      </select>
    </div>