【发布时间】:2020-08-09 09:24:54
【问题描述】:
当我尝试保存新问题(= Frage)时,我的 axios 请求(与 Laravel 结合)在 Web 控制台中出现 500 错误:
"Error: Request failed with status code 500"
VueJS 方法 save():
save: function(){
axios.post('/api/save-frage', this.Frage) //passes the object this.Frage
.then(res => {
// nothing here
});
}
api.php:
Route::post('/save-frage','FragenController@store');
FragenController.php(控制器):
public function store(Request $request)
{
// validation coming soon :)
$frage = new Frage;
$frage->Frage = request('Fragentext');
$frage->save(); //if I comment this out, there's no error 500 :)
}
Frage.php(模型):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Frage extends Model
{
protected $table = 'fragen';
protected $fillable = ['Frage']; // only field to fill is called "Frage"
}
我以为路由可能是错误的(api.php),但是如果我更改它,我会收到 404 错误,所以我想这是正确的,因为否则总是会出现 404 错误。 然后我检查了模型是否可能表或字段受到保护,但这对我来说看起来不错。 我在这里做错了什么?
【问题讨论】:
-
那么,你能展示一下你在 $request 中有什么吗?
dd($request->all()) -
status code 500是一个通用的“包罗万象”响应。检查/storage/logs/laravel.log文件以查看错误跟踪 -
你检查过网络检查器,XHR 选项卡吗?
-
使用 Insomnia 进行调试。它会指出错误。
-
您可以从响应中添加您的错误吗?您可以在网络标签中找到它