Aoudhesh Kumar
Aoudhesh Kumar

Reputation: 101

how to check count of likes and dislike for post for rating sysytem

i am making an system to check count of likes and dislike if like count is more then dislike count then it gives true but i am getting an error

// if (Files::withCount('likes') >= Files::withCount('dislike')) {
      //     return response()->json(['true']);

      // }elseif (Files::withCount('dislike') >= Files::withCount('like')) {
      //             return response()->json(['false']);
      // }else{
      //             return response()->json(['error'=>'somethingwenrwrng']);

      // }

  //   if( DB::table('files')->select('files_id')
  //       ->join('likes','files_id.files_id','=','files_id') > DB::table('files')->select('id')
  //       ->join('dislike','files_id.files_id','=','files_id') ){
  //     return response()->json(['true']);
  // }else {
  //   return response()->json(['error'=>'somethingwenrwrng']);
  // }
    $file = Files::find($id);
     if($file ->likes->count() > $file ->dislike->count() ){
      return response()->json(['true']);
    }else{
      return response()->json(['error'=>'somethingwenrwrng']);
    }

i have tried different method to check but getting an error

Upvotes: 1

Views: 133

Answers (1)

Ahmed Aboud
Ahmed Aboud

Reputation: 1322

the withCount() method return a property of the related field_count counting related models

so

$file = Files::find($id)->withCount(['likes','dislikes']);

if($file->likes_count > $file->dislikes_count ){
    return response()->json(['true']);
}

Upvotes: 2

Related Questions