每个帖子都喜欢和不喜欢按钮

时间:2018-03-22 20:38:17

标签: php jquery ajax laravel

伙计我刚刚为每项服务创建了喜欢/不喜欢的按钮!所以我为每一个设置了一个默认的灰色,一旦用户点击“喜欢”它变成绿色,而相反的情况下它变成红色..

所以我为此创建了一个ajax方法......

我想通过一个简单的警报检查一切是否正常() 但我得到一个错误:

  

消息:“Undefined property:Illuminate \ Database \ Eloquen \ Builder :: $ like”,...}
  异常:“ErrorException”
   - 文件:“C:\ xampp \ htdocs \ dawerelzirou \ app \ Http \ Controllers \ ServiceController.php”
   - 行:164   消息:“Undefined property:Illuminate \ Database \ Eloquent \ Builder :: $ like”
  追踪:[
   - {file:“C:\ xampp \ htdocs \ dawerelzirou \ app \ Http \ Controller \ ServiceController.php”,line:164}
  ]

这就像.js

$('.like').on('click', function() {
  var like_s = $(this).attr('data-like');
  var service_id = $(this).attr('data-serviceid');

  $.ajax({
    type: 'POST',
    url: url,
    data: {
      like_s: like_s,
      service_id: service_id,
      _token: token
    },
    success: function(data) {
      alert('ok');
    }
  });
});

这是剧本:

<script type="text/javascript" src="{{ asset('js/like.js') }}"></script>
<script type="text/javascript">
   var url="{{ route('like') }}";
   var token="{{ Session::token() }}";
</script>

以下是刀片页面:

@php 
$like_count=0;
$dislike_count=0;
$like_statut="grey";
$dislike_statut="grey";
@endphp

@foreach ($services->likes as $like)
  @php
    if ($like->like ==1){
      $like_count++;
    }
    else {
      $dislike_count++;
    }
    if($like->like == 1 && $like->user_id==Auth::user()->id){
      $like_statut="green";
    }
    else{
      $dislike_statut="red";
    }
  @endphp
@endforeach

<div class="row">
  <div class="col s12">
    <button type="button"
      data-like="{{$like_statut}}"
      data-serviceid="{{$services->id}}"
      class="like waves-effect waves-light btn {{$like_statut}}"> 
        <i class="material-icons left">thumb_up</i>({{$like_count}})
    </button>
  </div>
</div>
<br>
<div class="row">
  <div class="col s12">
    <button type="button"
      data-dislike="{{$dislike_statut}}"
      class="dislike waves-effect waves-light btn {{$dislike_statut}}"> 
        <i class="material-icons left">thumb_down</i>({{ $dislike_count}})
    </button>
  </div>
</div>
</div>
</div>

路线:

Route::post('/like','ServiceController@like')->name('like');

控制器中的“喜欢”方法:

public function like(Request $request)
{
  $like_s = $request->like_s;
  $service_id = $request->service_id;
  $like = Like::where(['service_id' => $service_id, 'user_id' => Auth::user()->id, ]);
  if (!$like) {
    $new_like = new Like;
    $new_like->service_id = $service_id;
    $new_like->user_id = Auth::user()->id;
    $new_like->like = 1;
    $new_like->save();

  } elseif ($like->like == 1) {
    Like::where(['service_id' => $service_id, 'user_id' => Auth::user()->id,])
      ->delete()
    ;

  } elseif ($like->like == 0) {
    Like::where(['service_id' => $service_id, 'user_id' => Auth::user()->id,])
      ->update(array('like' => 1))
    ;
  }
}

1 个答案:

答案 0 :(得分:0)

这一行:

$like = Like::where(['service_id' => $service_id, 'user_id' => Auth::user()->id, ]);

似乎不完整,因为它返回查询构建器而不是对象。查询构建器没有&#34; like&#34;属性。如果你想要一个对象你应该这样做:最后first()(或get()后跟foreach)似乎丢失了。