2017-10-03 11 views
0

まず、悪い英語をお詫び申し上げます。 laravel(バージョン5.4.24)を使用して、サイトの投稿用の投票ボタンを作成しています。jqueryとvalidateがlaravel5のクリックで機能しない

<button>を自分のコードに使用すると、画面上の投票ボタンをクリックしても機能しません。 代わりに、フォームを使用して値を渡しましたが、このメソッドは検証でjsonの値を適切に渡すようには見えません。

「ここ」の部分に問題が発生しました ArticleController.phpの「ここ」の部分を削除してボタンをクリックすると、上下の値がありません。私は「一部」を削除し、それを実行した場合 、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); 
} 

$記事=新しいアプリケーションの\条; $ 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) 
+0

私はそこに問題があると感じているからです。 –

+0

Dhaval Chheda //妥当性検査からin節を削除すると、ボタンをクリックするとSQLエラーが発生しました(SQLSTATE [42S22] :)。 – User8392

+0

php artisan tinkerでコードを試してみてください。検証がパスしているかのようにクエリに問題があるかどうかを確認してください(データベースにヒットしているようです)ので、確認して更新するか、クエリの最後にtoSqlそれはSQLクエリをエコーし​​、そこからクエリをデバッグすることができます –

答えて

0

あなたは、データベーステーブル内のフィールドを持っていないか、またはあなたが以下のようにモデル内の任意のフィールドを追加していなかったので、それがあります。

protected $fillable = [ 
    'user_id', 
    'up', 
    'down', 
    'voted_at', 
]; 
+0

あなたの返事をありがとう、申し訳ありませんが、データベースにフィールドがあります。 – User8392

1

問題は、あなたがsum()がどのように動作するかを確認するには、この

return response()->json([ 
     'voted' => $request->input('vote'), 
     'value' => $article->votes()->sum('vote'), 
    ], 201, [], JSON_PRETTY_PRINT); 
} 

Click hereのようにフィールド名を渡す必要がどこにここ$requestのインスタンスを渡しているここにあるあなたがを削除するときに何が起こるか

+0

ありがとう!私はそれを解決した – User8392

+0

喜び仲間私はあなたが自己によって答えを見つけたと思われる? –

+0

回答セクションに回答を追加してくださいありがとうございます。 –

関連する問題