values of columns that is empty returns null. How to I make turn the null to zero so that if the value is null it returns zero.
public function getThumbsUpAttribute($value){ return $value !== null ? $value : 0; } public function videos() { // return VideoViewsLikes::all(); $rec_videos = Videos::orderBy('videos.created_at', 'DESC')->where('videos.publish', 1) ->leftJoin('video_views_likes', 'videos.vid_id', '=', 'video_views_likes.vid_id') ->select('videos.vid_id', 'videos.title as title', DB::raw("sum(video_views_likes.thumbs_down) as dislike"), DB::raw("sum(video_views_likes.views) as views"), DB::raw("sum(video_views_likes.thumbs_up) as likes")) ->groupby('videos.vid_id') ->groupby('videos.title') ->get(); return response()->json(['success' => $rec_videos]); }
it still returns null.
Advertisement
Answer
instead of:
DB::raw("sum(video_views_likes.thumbs_down) as dislike");
you can use COALESCE witch Return the first non-null value
DB::raw(" COALESCE(sum(video_views_likes.thumbs_down),0) as dislike");