Home > Enterprise >  laravel form is returning to same page
laravel form is returning to same page

Time:11-11

I'm quickly becoming confident in Laravel, but this one has me stumped. I've successfully created a number of forms that validate and save the data before moving to a new page, but this one is just refreshing the page and I can't see why.

I'd like the dashboard page to ask the user for a date, then display available bookings for that date in the bookings.index page.

dashboard.blade.php includes:

<form method="POST" action="{{ route('bookings.index') }}">
    @csrf

    <!-- Select Date -->
    <div>
        <x-input-label for="date" :value="__('Date')" />

        <x-text-input id="date"  type="date" name="date" :value="old('date')" required autofocus />

        <x-input-error :messages="$errors->get('date')"  />
    </div>
    <div>
        <x-primary-button >
            {{ __('Find Available Fields') }}
        </x-primary-button>
    </div>
</form>

Booking Controller includes:

<?php

namespace App\Http\Controllers;

use App\Model\User;
use App\Models\Booking;
use App\Models\Field;
use App\Models\Timeslot;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $validated = $request->validate([
            'date'    => 'required|date|after:yesterday',
        ]);

        redirect(route('bookings.index', [
            'fields' => Field::get(),
            'timeslots' => Timeslot::get(),
            'date' => $request->date,
        ]));
    }
}

web.php includes:

Route::get('/dashboard', function () {
    return view('dashboard', [
        'fields' => Field::get(),
        'timeslots' => Timeslot::get(),
        'bookings' => auth()->user()->bookings(),
    ]);
})->middleware(['auth', 'verified'])->name('dashboard');
Route::resource('bookings', BookingController::class)
    ->only(['index', 'store', 'edit', 'update', 'destroy'])
    ->middleware(['auth', 'verified']);

When I enter a date and click on the 'Find Available Fields' button, I expect to be sent to the bookings/index page, but instead the dashboard page refreshes. I can't see anything in the above code that would cause that, so I wonder if I need to look somewhere else. Any guidance would be greatly appreciated.

Edit: Since the submit request never reaches the bookings/index page, I'm including the actual source code of the form. Again, it looks good to me so I don't know why the controller code isn't triggering.

    <form method="POST" action="http://localhost:8000/bookings">
        <input type="hidden" name="_token" value="NTw8hEadjvDv0HIlYDSZNmFFi0e6U9O9UzxfRy13">
        <!-- Select Date -->
        <div>
            <label  for="date">Date</label>
            <input   id="date" type="date" name="date" required="required" autofocus="autofocus">
                        </div>
        <div>
            <button type="submit" >Find Available Fields</button>
        </div>
    </form>

CodePudding user response:

This is because of a wrong redirect syntax(also you need to pass a return keyword). To redirect to a named route, you can't pass it as a parameter in the redirect() helper. Rather you should do it this way:

return redirect()->route('my.route');

or simply to_route('my.route');

You can also pass your variables as a second parameter to these helpers.

That said, change your booking controller code to this:

<?php

namespace App\Http\Controllers;

use App\Model\User;
use App\Models\Booking;
use App\Models\Field;
use App\Models\Timeslot;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $validated = $request->validate([
            'date'    => 'required|date|after:yesterday',
        ]);

        return redirect()->route('bookings.index', [
            'fields' => Field::get(),
            'timeslots' => Timeslot::get(),
            'date' => $request->date,
        ]);
    }
}

For more info, take a look at the docs

  • Related