我的代码有问题。当我尝试使用Vue.JS和Laravel存储数据时,控制台出现错误500。我在上面坚持了6个小时。
控制器 -DashboardAdvantagesController.php:
public function store(Request $request)
{
$request->validate([
'icon'=> 'required',
'title'=> 'required'
]);
$advantage = $request->HomepageAdvantages()->create([
'icon'=> $request->icon,
'title'=> $request->title
]);
return response()->json([
'advantage'=> $advantage,
'message'=> 'task has been created!'
]);
}
型号 -HomepageAdvantages
class HomepageAdvantages extends Model
{
protected $fillable = [
'icon', 'title', 'text',
];
}
API路由
Route::post('/advantages/store', 'DashboardAdvantagesController@store');
Vue组件
createAdvantage(){
console.log(this.advantage.icon);
console.log(this.advantage.title);
axios.post('http://127.0.0.1:8000/api/advantages/store', {icon: this.advantage.icon,
title: this.advantage.title})
.then(response=>{
this.advantages.push(response.data.advantage);
})
.catch(error=>{
console.log(error);
});
console.log(response.data.advantage);
},
我的错误:
无法加载资源:服务器响应状态为500 (内部服务器错误)
:8000/api/advantages/store:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
2app.js:267 POST http://127.0.0.1:8000/api/advantages/store 500 (Internal Server Error)
dispatchXhrRequest @ app.js:267
xhrAdapter @ app.js:118
dispatchRequest @ app.js:706
Promise.then (async)
request @ app.js:513
Axios.<computed> @ app.js:533
wrap @ app.js:966
createTask @ app.js:1773
invokeWithErrorHandling @ app.js:9448
invoker @ app.js:9773
original._wrapper @ app.js:15126
答案 0 :(得分:0)
$request->HomepageAdvantages()
我不确定您是如何提出的,因为只有在Request
类上注册了宏时,它才起作用。如果是这样,请忽略此评论。
否则,您需要使用HomepageAdvantages::create()
明确调用模型。