skip to Main Content

I’m developing a Laravel 10 application where users can add stages to a trip. I have a form in the travel show.blade.php file that allows creating a new stage. When I submit the form, the page reloads, but no new entry is created in the database, and it seems like the store() method in the controller isn’t being executed.

Context:

Form in the Blade file (show.blade.php):

<form action="{{ route('user.stages.store', $travel->slug) }}" method="POST"
        class="add_stage_form w-100 d-none py-3">
        @csrf

        <input type="hidden" name="travel_id" value="{{ $travel->id }}">


        <div class="input_wrapper mb-1 row">
          <label for="place" class="input_label">{{ __('Tappa') }}</label>

          <div class="col-md-6">
            <input id="place" type="text" class="form-control @error('place') is-invalid @enderror" name="place"
              value="{{ old('place') }}" required autocomplete="place" autofocus>

            @error('place')
              <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
              </span>
            @enderror
          </div>
        </div>

        <div class="input_wrapper mb-3 row">
          <label for="note" class="input_label">{{ __('Note') }}</label>

          <div class="col-md-6">
            <textarea id="note" name="note" class="form-control @error('note') is-invalid @enderror" autofocus>
            {{ old('note') }}
            </textarea>

            @error('note')
              <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
              </span>
            @enderror
          </div>
        </div>

        <div class="d-flex align-items-center gap-3">

          <button type="submit" class="btn button_style btn_primary">
            Salva tappa
          </button>

          <button type="button" class="close_form_btn btn button_style btn_secondary">
            Elimina tappa
          </button>

        </div>
      </form>

Controller (StageController.php):

public function store(StoreStageRequest $request, Travel $travel)
{
    // Validate
    $val_data = $request->validated();

    // Set the travel_id
    $val_data['travel_id'] = $travel->id;

    // Create slug
    $slug = Str::slug($request->place, '-');
    $val_data['slug'] = $slug;

    // Handle photo upload if present
    if ($request->hasFile('photo')) {
        $image_path = Storage::put('uploads', $request->file('photo'));
        $val_data['photo'] = $image_path;
    }

    // Create the stage
    Stage::create($val_data);

    dd('Store method reached', $request->all(), $travel);

    // Redirect to the trip's show page
    return redirect()->route('user.travels.show', $travel->slug)->with('message', 'New stage added!');
}

Request Validation Class (StoreStageRequest.php):

public function authorize(): bool
{
    return true;
}

public function rules(): array
{
    return [
        'place' => 'required|min:5|max:150',
        'travel_id' => 'required|exists:travel,id',
        'note' => 'nullable|max:250',
        'photo' => 'nullable|image|max:2000'
    ];
}

When I submit the form:

The page reloads, but no new entry is created in the stages table in the database.
I don’t see the output of the dd() I placed at the beginning of the store() method, which suggests the method isn’t being triggered.
There are no visible errors on the page, and the form seems to be submitted correctly.

Question:

Why isn’t the store() method in the controller being executed, and why does the page reload without saving data to the database? How can I fix this issue and ensure the method is correctly triggered?

Thanks in advance for any help!

What I’ve Tried:

  • I verified that the POST route is correctly configured in the web.php file:
route::post('/travels/{travel}/stages', [StageController::class, 'store'])->name('stages.store');
  • I made sure that the CSRF token is present in the form.

  • I monitored the POST request using the browser’s developer tools and observed that the request is being sent to the correct URL (http://127.0.0.1:8000/user/travels/{slug}/stages) with a status code of 302.

2

Answers


  1. I think there might be an issue with the action within your form..
    Do something like this

    action="{{ route('stages.store', $travel->slug) }}" 
    

    then in the store method dry dump the request

    dd($request->all());
    

    If it shows the debug information then congrats the method is executed well..
    Then check your submitted values like

    dd($request->text);
    dd($request->note);
    
    
    Login or Signup to reply.
  2. You dont need to validate the presence of travel_id since you are using the slug present in route to identify the Travel in question and if not present or the Travel is not existing in the DB, a 404 will be triggered.

    So, just remove the travel_id from validation if you are not using StoreStageRequest::class anywhere else.

    If you are using it somewhere else, it’s better to make a new Form Requests Class without travel_id rules

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search