[英]jquery and validate does not work on click in laravel5
首先,我為英語不好對不起。 我現在使用laravel(版本5.4.24)為網站帖子創建投票按鈕。
如果我僅在代碼中使用<button>
,則在單擊屏幕上的“投票”按鈕時將無法使用。 因此,作為替代,我使用表單傳遞了值,但此方法似乎在驗證中未正確傳遞json的值。
該問題發生在“這里”部分,如果我刪除ArticleController.php
的“這里”部分並單擊按鈕,則它沒有向上或向下的值。 如果刪除“ 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 =新的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)
問題出在這里,您需要在此處傳遞$request
實例,在此處您需要傳遞字段名稱
return response()->json([
'voted' => $request->input('vote'),
'value' => $article->votes()->sum('vote'),
], 201, [], JSON_PRETTY_PRINT);
}
單擊此處查看sum()
工作方式
這是因為您可能沒有數據庫表中的字段,或者您沒有在模型中添加任何字段,如下所示。
protected $fillable = [
'user_id',
'up',
'down',
'voted_at',
];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.