I have my api.php setup so it goes:
use App\Http\Controllers\CalendarController;
use Illuminate\Http\Request;
Route::post("/home",[CalendarController::class, 'add']);
Route::post('/event/create', [CalendarController::class, 'add']);
I then have it set up so in my home.blade.php it fetches the api/event/create endpoint and attempts to post to it:
const csrfToken = document.head.querySelector("[name~=csrf-token][content]").content;
console.log(csrfToken);
fetch('/api/event/create', {
method: 'POST',
headers: {
"X-CSRF-Token": csrfToken
},
body: encodeFormData(eventData)
})
Issue is that when the code is run, it outputs: https://imgur.com/a/1OFpfST Are there any errors within my code? I am able to log the csrfToken successfully though, but am not sure if it passes on to the server correctly. Thanks all for any help.
Output in laravel.log:
[2022-03-01 17:04:14] local.ERROR: Method Illuminate\Http\Request::show does not exist. {"exception":"[object] (BadMethodCallException(code: 0): Method Illuminate\Http\Request::show does not exist. at C:\xampp\htdocs\Room Booking System VUE\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:113)
The CalendarController in my case has a setup of this, where I'm not sure if it is working with 'Request' properly:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Booking;
class CalendarController extends Controller
{
public function index()
{
$events = array();
$bookings = Booking::all();
foreach($bookings as $booking) {
$events[] = [
'title' => $booking->title,
'resourceId' =>$booking->resourceId,
'start' => $booking->start_date,
'end' => $booking->end_date,
];
}
return view('home', ['events' => $events]);
}
}
CodePudding user response:
use Illuminate\Http\Request;
use Path\To\EventController;
Route::post('/event/create', [EventController::class, 'store']);
You should put EventController::class (or whatever your controller class) and second array element should be your method inside this controller.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Booking;
use Illuminate\Support\Facades\Request;
class CalendarController extends Controller
{
public function index()
{
$events = array();
$bookings = Booking::all();
foreach($bookings as $booking) {
$events[] = [
'title' => $booking->title,
'resourceId' =>$booking->resourceId,
'start' => $booking->start_date,
'end' => $booking->end_date,
];
}
return view('home', ['events' => $events]);
}
public function store(Request $request){
dd($request);
}
}