Reputation: 459
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" class="block mt-1 w-full" type="date" name="date" :value="old('date')" required autofocus />
<x-input-error :messages="$errors->get('date')" class="mt-2" />
</div>
<div>
<x-primary-button class="ml-3">
{{ __('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 class="block font-medium text-sm text-gray-700" for="date">Date</label>
<input class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full" id="date" type="date" name="date" required="required" autofocus="autofocus">
</div>
<div>
<button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150 ml-3">Find Available Fields</button>
</div>
</form>
Upvotes: 0
Views: 661
Reputation: 830
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
After further discussion and research, the primary problem is that the form was set to 'POST' while an index page requires the 'GET' method. Check this answer.
Upvotes: 1