首先,我为糟糕的英语道歉。 我现在正在使用laravel(版本5.4.24)为站点帖子创建一个投票按钮。
如果我在代码中使用<button>
,则单击屏幕上的投票按钮时它不起作用。
所以,作为替代方案,我使用表单传递了值,但是这个方法似乎没有在验证中正确传递json的值。
问题出现在“这里”部分,
如果我删除ArticleController.php
的“here”部分并单击该按钮,则它没有up或down值。
如果我删除“part”并运行它,laravel将显示以下错误:
SQLSTATE [42S22]: Column not found: 1054 Unknown column '' in 'field list' (SQL: SELECT sum (` `) as aggregate from` votes` where `votes`.`article_id` = 107 and` votes`. `article_id` is not null)
我一直在寻找一种方法,但我找不到答案。
感谢您的帮助。谢谢。
show.blade.php
//<form ...> </ form> is code that was not in the example, but if I click on
<button> without it, there is no response on the screen.(No redirects)
<div class="action__article">
<form action="{{ route('videos.vote', $article->id) }}" method="post">
@if ($currentUser)
{!! csrf_field() !!}
<button class="btn__vote__article" data-vote="up" title="{{ trans('forum.comments.like') }}" {{ $voted }}>
<i class="fa fa-heart"></i>
<span>{{ $article->up_count }}</span>
</button>
@endif
</form>
</div>
* index.blade.php
@section('script')
@parent
<script type="text/javascript" charset="utf-8">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('vote')
}
});
$('.btn__vote__article').on('click', function(e) {
var self = $(this),
articleId = $article['id'];
$.ajax({
type: 'POST',
url: '/video/' + articleId + '/votes',
data: {
vote: self.data('vote')
}
}).then(function (data) {
self.find('span').html(data.value).fadeIn();
self.attr('disabled', 'disabled');
self.siblings().attr('disabled', 'disabled');
});
});
</script>
@endsection
* ArticleController.php
public function vote(Request $request, \App\Article $article)
{
//"here" - The value received from the form will not pass here and will be redirected to the previous page.
$this->validate($request, [
'vote' => 'required|in:up,down',
]);
if ($article->votes()->whereUserId($request->user()->id)->exists()) {
return response()->json(['error' => 'already_voted'], 409);
}
$up = $request->input('vote') == 'up' ? true : false;
$article->votes()->create([
'user_id' => $request->user()->id,
'up' => $up,
'down' => ! $up,
'voted_at' => \Carbon\Carbon::now()->toDateTimeString(),
]);
return response()->json([
'voted' => $request->input('vote'),
'value' => $article->votes()->sum($request->input('vote')),
], 201, [], JSON_PRETTY_PRINT);
}
$ article = new App \ Article; $ request = new App \ Http \ Requests \ ArticlesRequest;
*模型
class Vote extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'up',
'down',
'voted_at',
];
/**
* The attributes that should be visible in arrays.
*
* @var array
*/
protected $visible = [
'user_id',
'up',
'down',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'voted_at',
];
/* Relationships */
public function articles()
{
return $this->belongsTo(Article::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
/* Mutators */
public function setUpAttribute($value)
{
$this->attributes['up'] = $value ? 1 : null;
}
public function setDownAttribute($value)
{
$this->attributes['down'] = $value ? 1 : null;
}
}
*数据库
MariaDB [mmdance]> desc votes
-> ;
+------------+------------------+------+-----+-------------------+---------------------------- -+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+---------------------------- -+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| article_id | int(10) unsigned | NO | MUL | NULL | |
| up | tinyint(4) | YES | | NULL | |
| down | tinyint(4) | YES | | NULL | |
| voted_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------------+------+-----+-------------------+---------------------------- -+
6 rows in set (0.00 sec)
答案 0 :(得分:1)
问题在于你在这里传递$request
的实例,你需要传递这样的字段名称
return response()->json([
'voted' => $request->input('vote'),
'value' => $article->votes()->sum('vote'),
], 201, [], JSON_PRETTY_PRINT);
}
Click here了解sum()
如何运作
答案 1 :(得分:0)
这是因为您可能没有数据库表中的字段,或者您没有在下面的模型中添加任何字段。
protected $fillable = [
'user_id',
'up',
'down',
'voted_at',
];