【发布时间】:2017-10-05 14:53:12
【问题描述】:
所以我试图将数据存储在我的election_users 表中,但发出 POST 请求会出现“500(内部服务器错误)”错误。我的对象确实返回了正确的“election_id”。我不知道我做错了什么。
这是我的 vue 脚本中的方法:
methods: {
createVote: function () {
var itemId = this.$route.params.id
var input = this.newUserVoted
this.newUserVoted.election_id = itemId
this.$http.post('http://www.nmdad2-05-elector.local/api/v1/electionuser', input)
.then((response) => {
this.newUserVoted = {'election_id': itemId}
}).catch(e => {
console.log(e)
console.log(input)
})
}
},
我的 UsersController.php
public function store(Request $request)
{
$userVoted = new ElectionUser();
$userVoted->election_id = $request['election_id'];
$userVoted->user_id = Auth::id();
if ($userVoted->save()) {
return response()
->json($userVoted);
}
}
我的路由Api.php
Route::post( 'electionuser', 'UsersController@store' );
我的 ElectionUser.php
class ElectionUser extends Model
{
// Relationships
// =============
protected $fillable = [
'election_id',
'user_id'
];
public function election()
{
return $this->belongsToMany(Election::class);
}
}
【问题讨论】:
-
如果您可以发布页面给出的错误,那就太好了。这可能是 CSRF 错误,或者您忘记使用 Auth 或许多其他东西。 (按 f12 进入网络)
-
它说“在 VerifyCsrfToken.php 第 68 行中出现 TokenMismatchException:”