Reputation: 1
Recently I'm using Laravel to develop my booking service applications.
I had a problem when I wanted to store the data into the database through the form page. I already set the route, and method correctly, but the problem starts when I click the submit button. The form was reloaded and didn't save the data that I already inputted before.
This is the blade form for the interface :
<form method="POST" action="{{ route('front.appointment_store') }}"
class="flex flex-col p-[30px] rounded-[20px] gap-[18px] bg-white shadow-[0_10px_30px_0_#D1D4DF40] w-full md:w-[700px] shrink-0">
@csrf
....
<button type="submit"
class="bg-cp-dark-blue p-5 w-full rounded-xl hover:shadow-[0_12px_30px_0_#312ECB66] transition-all duration-300 font-bold text-white">Book
Appointment</button>
</form>
And this is for route file in web.php
Route::get('/', [FrontController::class, 'index'])->name('front.index');
Route::get('/team', [FrontController::class, 'team'])->name('front.team');
Route::get('/about', [FrontController::class, 'about'])->name('front.about');
Route::get('/appointment', [FrontController::class, 'appointment'])->name('front.appointment');
Route::post('/appointment/store', [FrontController::class, 'appointment_store'])->name('front.appointment_store');
As you can see, data from blade file is referring to appointment_store method.
And this is for appointment_store method that i save in Controller :
public function appointment_store(StoreAppointmentRequest $request)
{
DB::transaction(function () use ($request) {
$validated = $request->validated();
$newAppointment = Appointment::create($validated);
// dd($newAppointment);
});
return redirect()->route('front.index');
}
Honestly i had a stuck when troubleshooting this bug, since all the method, variable, and route are pronounced correctly, and when i try to debug it using dd($newAppointment);, it didn't show any data that being saved into the form.
Please provide the solutions for this bug, so the data can be stored into database, and the form can be moved to Index file as written in appointment_store method. Thank you.
Upvotes: 0
Views: 53
Reputation: 278
I happen to have had the same problem before The problem was verifying the request. I don't know what are you trying to submit. However, try dump the validated object returned by this line:
$validated = $request->validated();
add this line directly below it:
dd($validated);
or use debug to see if the $validated object is successfully returned.
Upvotes: 0
Reputation: 473
If the page is just reloading, is probably some error with the form validation (caused by the StoreAppointmentRequest).
To confirm (fastly), add:
<?php dump($errors); ?>
on the .blade file with the form.
and submit the form again.
If this is indeed the case, then: https://laravel.com/docs/11.x/validation#quick-displaying-the-validation-errors
Upvotes: 0
Reputation: 66
You should check your model $fillable and $guard variables which is built in variable for Mass Assignment.
Upvotes: 1