'Laravel perform route action on select dropdown onchange, create specific URL
I've got Dropdown select with the list with future 12 months:
<select name="month" id="specificMonth">
@foreach(Carbon\CarbonPeriod::create(now()->startOfMonth(), '1 month', now()->addMonths(11)->startOfMonth()) as $date)
<option value="{{ $date }}">
{{ $date->format('F Y') }}
</option>
@endforeach
</select>
If the option in dropdown changes, I want to perform route action like:
$('#specificMonth').change(function(){
var data = $(this).serialize();
alert(data);
$.ajax({
type: "POST",
URL: "{{ route('calendar.specificMonth', 'date') }}",
data: data,
success: function () {
console.log("works");
}
});
});
So I need to get the selected month in Controller and return view with specific month. How I can do that?
When I "alerts" data it shows it correctly.
URL in my case should look like:
/calendar/{date}
Or something like that. For now my route for that looks like:
Route::post(
'/calendar/{date}',
[CalendarController::class, 'specificMonth']
)->name('calendar.specificMonth');
But performing an action in view gives me an error "POST http://127.0.0.1:8000/calendar 405 (Method Not Allowed)".
This should be POST or GET method? How to create custom URL in that case?
Updated, Method from Controller in which I want to dump data only:
public function specificMonth(Request $request, $date)
{
dd($request);
}
Solution 1:[1]
You cannot dump data while using ajax. You can use Illuminate\Support\Facades\Log::debug($request) in the controller. Then the request will be shown in the storage/logs/logfielname.log.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mátyás Gr?ger |
