我怎样才能成功地将这条通往 post 的路线连接到我的数据库?获取错误 500
How would I get this Route to post to my database successfully? Getting error 500
我有我的 api.php 设置,所以它是:
use App\Http\Controllers\CalendarController;
use Illuminate\Http\Request;
Route::post("/home",[CalendarController::class, 'add']);
Route::post('/event/create', [CalendarController::class, 'add']);
然后我设置它,所以在我的 home.blade.php 中它获取 api/event/create 端点并尝试 post 到它:
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)
})
问题是,当代码为 运行 时,它会输出:
https://imgur.com/a/1OFpfST
我的代码中有任何错误吗?虽然我能够成功记录 csrfToken,但不确定它是否正确传递到服务器。感谢大家的帮助。
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)
我的 CalendarController 有这样的设置,我不确定它是否与 'Request' 一起正常工作:
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]);
}
}
use Illuminate\Http\Request;
use Path\To\EventController;
Route::post('/event/create', [EventController::class, 'store']);
您应该放置 EventController::class(或任何您的控制器 class),第二个数组元素应该是您在此控制器中的方法。
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);
}
}
我有我的 api.php 设置,所以它是:
use App\Http\Controllers\CalendarController;
use Illuminate\Http\Request;
Route::post("/home",[CalendarController::class, 'add']);
Route::post('/event/create', [CalendarController::class, 'add']);
然后我设置它,所以在我的 home.blade.php 中它获取 api/event/create 端点并尝试 post 到它:
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)
})
问题是,当代码为 运行 时,它会输出: https://imgur.com/a/1OFpfST 我的代码中有任何错误吗?虽然我能够成功记录 csrfToken,但不确定它是否正确传递到服务器。感谢大家的帮助。
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)
我的 CalendarController 有这样的设置,我不确定它是否与 'Request' 一起正常工作:
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]);
}
}
use Illuminate\Http\Request;
use Path\To\EventController;
Route::post('/event/create', [EventController::class, 'store']);
您应该放置 EventController::class(或任何您的控制器 class),第二个数组元素应该是您在此控制器中的方法。
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);
}
}