我正在尝试使用laravel控制器将一些数据从vue组件发送到数据库。为了避免出现空白输入错误,我已经使除ID外的所有表列都可为空。仍然,当我尝试使用post方法发送数据时,出现了错误500。
这是我的vue方法:
addRecord(){
fetch('api/video_call', {
method: 'post',
body: JSON.stringify(this.call_detail),
headers: {
'content-type': 'application/json'
}
})
.then(res => res.json())
.then( data=> {
this.call_detail.receiver_caller_id = '';
this.call_detail.sender_caller_id = '';
this.call_detail.receiver_id= '';
this.call_detail.sender_id= '';
this.call_detail.call_received='';
alert('Test Done');
this.fetchCallDetail();
})
.catch(err => console.log(err));
}
}
这是我从vue返回的数据:
data(){
return {
users: [],
call_details: [],
call_detail: {
id: '',
receiver_caller_id: '',
sender_caller_id: '',
receiver_id: '',
sender_id: '',
call_received: ''
},
call_detail_id: '',
pagination: {},
call_allowed: false
}
}
这是我在api.php中的路线
Route::post('video_call', 'VideoCallController@store');
最后是控制器中的存储功能:
public function store(Request $request)
{
$record = $request->isMethod('put')?VideoCall::findOrFail($request->call_detail_id):new VideoCall;
$record->id= $request->input('call_detail_id');
$record->receiver_id= $request->input('receiver_id');
$record->sender_id= $request->input('sender_id');
$record->sender_call_id= $request->input('sender_call_id');
$record->receiver_call_id= $request->input('receiver_call_id');
$record->call_recieved=$request->input('call_received');
if($record->save())
{
return new VideoCallResource($record);
}
}
我在以前的应用程序中使用了这种方法,将数据从vue发送到数据库,并且效果很好。在此应用中,它返回错误。