Laravel query builder count - php

I'm using Laravels query builder to retrieve a list of items with some filter options - I need to do a count inside of this query:
$f = DB::table('Likes')
->join('Freestyle', 'Likes.FreestyleID', '=', 'Freestyle.id')
->join('Beat', 'Freestyle.BeatId', '=', 'Beat.id')
->join('Track', 'Beat.TrackId', '=', 'Track.id')
->join('Genre', 'Track.GenreId', '=', 'Genre.id')
->select('Likes.freestyleID as likeFreestyleID', 'Freestyle.*', 'Beat.TrackId as UseMeForTrack',
'Genre.id as GenreID')
->where('Freestyle.Active', '1')
->where('Freestyle.created_at', '>', "$dateScope")
->whereNull('Freestyle.deleted_at')
->whereIn('GenreID', $request->genre)
->first();
To count the amount of times the 'FreestyleID' appears in the likes table.
is this possible? The data returned is perfect I just need the amount of likes a freestyle has, where the FreestyleID in the likes table is null.

Something like this :
$f = DB::table('Likes')
->join('Freestyle', 'Likes.FreestyleID', '=', 'Freestyle.id')
->join('Beat', 'Freestyle.BeatId', '=', 'Beat.id')
->join('Track', 'Beat.TrackId', '=', 'Track.id')
->join('Genre', 'Track.GenreId', '=', 'Genre.id')
->select('Likes.freestyleID as likeFreestyleID','count(Likes.FreestyleID)', 'Freestyle.*', 'Beat.TrackId as UseMeForTrack',
'Genre.id as GenreID')
->where('Freestyle.Active', '1')
->where('Freestyle.created_at', '>', "$dateScope")
->whereNull('Freestyle.deleted_at')
->whereIn('GenreID', $request->genre)
->first();

I think you should be able to use a raw expression like this:
$f = DB::table('Likes')
->join('Freestyle', 'Likes.FreestyleID', '=', 'Freestyle.id')
->join('Beat', 'Freestyle.BeatId', '=', 'Beat.id')
->join('Track', 'Beat.TrackId', '=', 'Track.id')
->join('Genre', 'Track.GenreId', '=', 'Genre.id')
->select(DB::raw('COUNT(likes.FreestyleID) as num_likes'), 'Likes.freestyleID as likeFreestyleID', 'Freestyle.*', 'Beat.TrackId as UseMeForTrack',
'Genre.id as GenreID')
->where('Freestyle.Active', '1')
->where('Freestyle.created_at', '>', "$dateScope")
->whereNull('Freestyle.deleted_at')
->whereIn('GenreID', $request->genre)
->groupBy('Freestyle.id')
->first();

Related

laravel join query with two table each have two where condition

this is my controller and i need to add one where condition on table chapters with is_enabled = 0
$module = $course->modules()
->with('chapters')
->join('course_student', 'course_student.module_id', '=', 'modules.id')
->select('*', DB::raw('course_student.module_id as id'))
->where('is_enabled', 1)
->where('course_student.student_id', $user->id)
->where('course_student.test_completed', 0)
->where('course_student.ends_on', '>', Carbon::now())
->orderBy('order')
->first();
You should use the whereHas method if you want conditional selection of relationships.
These methods allow you to add customized constraints to a
relationship constraint, such as checking the content of a comment
Relationships documentation
Leaving you with a code like:
$module = $course->modules()
->whereHas('chapters', function ($query) {
$query->where('is_enabled', 0);
})
->join('course_student', 'course_student.module_id', '=', 'modules.id')
->select('*', DB::raw('course_student.module_id as id'))
->where('is_enabled', 1)
->where('course_student.student_id', $user->id)
->where('course_student.test_completed', 0)
->where('course_student.ends_on', '>', Carbon::now())
->orderBy('order')
->first();

Laravel Query where All

I wondered how to make a Where All clause with Laravel
I'm trying to check if the episodes that a user saw are all the episodes of the series.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$alleps get all the episodes of the serie
$seriessaw get all the episodes a user saw
Thank you for your answers !
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I think you would need to set a having clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes table.
When you use WhereIn, you have to pass an array as the second parameter.
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id)->get()->toArray();
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();

Laravel query builder - Raw statements

Struggling to get my head around raw statements I have the following:
$f = DB::table('Likes')
->join('Freestyle', 'Likes.FreestyleID', '=', 'Freestyle.id')
->join('User', 'Freestyle.ParticipantId', '=', 'User.id')
->join('Beat', 'Freestyle.BeatId', '=', 'Beat.id')
->join('Track', 'Beat.TrackId', '=', 'Track.id')
->join('Genre', 'Track.GenreId', '=', 'Genre.id')
->select('Likes.*', 'Freestyle.*', 'Beat.TrackId as UseMeForTrack', 'Genre.id as GenreID', 'Track.Title as TrackTitle', 'User.slug', 'User.Username')
->addSelect(DB::raw('COUNT(Likes.FreestyleID) as freestyle_likes'))
->whereNull('Likes.deleted_at')
->groupBy('Freestyle.id')
->where('Freestyle.Active', '1')
->where('Freestyle.created_at', '>', "$dateScope")
->whereNull('Freestyle.deleted_at')
->whereIn('GenreID', $request->genres)
->get();
And this returns a count of the amount of likes my freestyle has as well as all of the information / filters. It's working great.
I need to do a check to see if my Auth::id(); exists inside of the Likes.UserId column where the Likes.FreestyleID = Freestyle.id. Essentially I'm checking whether or not the user likes the freestyle so I can update my front end. Is this something I can achieve with DB::raw?

selects, joins, & wheres in Laravel

I'm trying to select specific columns from two tables however when I add the ->select() method into my query, I get an error.
If I leave out the ->select() method, I get a valid resultset and everything works, but adding the select breaks it. Sadly the error reported has nothing to do with the query and is useless to me.
Here is the code that works:
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->get();
Now here's the code that breaks:
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->select(DB::raw('notifications.id, notifications.subject, notifications.message, notifications.url,
notifications.start_date, notifications.end_date, notifications.access_role_id, notifications_pivot.id,
notifcations_pivot.notification_id, notifications_pivot.user_id, notifications_pivot.is_read'))
->get();
It's times like these when I wish I could just write straight SQL and parse the query!
Any suggestions?
Take out the DB::raw() and just pass the fields you want as parameters.
If that doesn't work, the Laravel log at app/storage/logs/laravel.log may provide more insight.
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->select('notifications.id', 'notifications.subject', 'notifications.message', 'notifications.url', 'notifications.start_date', 'notifications.end_date', 'notifications.access_role_id', 'notifications_pivot.id', 'notifcations_pivot.notification_id', 'notifications_pivot.user_id', 'notifications_pivot.is_read')
->get();

kohana query show latest record only

OK so basically I am creating a pm inbox in my kohana project
So far I have been able to retrieve the messages without an issue with this query
$messages = DB::select('users.username', 'users.id', 'profiles.profile_picture', 'messages.thread', 'messages.subject', 'messages.content', array('messages.id', 'mid'))->from('messages')->join('users', 'LEFT')->on('users.id', '=', 'messages.from_id')
->join('profiles', 'LEFT')->on('profiles.user_id', '=', 'messages.from_id')->where('messages.to_id', '=', $user)->and_where('messages.deleted', '=', '0')->execute();
My question is I would like to only show the last message if there is multiple from the same user. Should this be done in the query or with php when rendering the results.
I have a date column I think I can use for this, but I am not sure the best approach.
Thanks
$messages = DB::select('users.username', 'users.id', 'profiles.profile_picture', 'messages.thread', 'messages.subject', 'messages.content', array('messages.id', 'mid'))
->from('messages')
->join('users', 'LEFT')->on('users.id', '=', 'messages.from_id')
->join('profiles', 'LEFT')->on('profiles.user_id', '=', 'messages.from_id')
->where('messages.to_id', '=', $user)
->and_where('messages.deleted', '=', '0')
->order_by('messages.id', 'desc')
->offset(0)
->limit(1)
->execute();

Categories