Laravel query builder - Raw statements - php

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?

Related

Laravel how to get only one record per user

In my laravel-application, I want to display all users/candidates, which have taken an education. Now it might happen, that a user/candidate has taken more than one education, so in this case, just the latest education should be displayed.
I've come to this:
$users = User::whereHas('roles', function ($q) {
$q->where('slug', 'candidate');
}
)->whereHas('educations')
->join('user_educations', 'user_educations.user_id', '=', 'users.id')
->join('educations', 'user_educations.education_id', '=', 'educations.id')
->join('education_levels', 'user_educations.education_level_id', '=', 'education_levels.id')
->select('users.id', 'users.name', 'users.surname', 'users.status', 'users.city', 'users.zipcode', 'users.birthday', 'users.avatar', 'educations.title as education', 'education_levels.title as education_level')
->where('user_educations.user_id', '=', 'users.id') // HERE IT FAILS - returns null
->first();
return response(['success' => true, "users" => $users], 200);
when I leave out the where('user_educations.user_id', '=', 'users.id')-clause, and do get() instead of first(), I get all users/candidates with educations, and also sometimes the same user multiple times, depending on how many educations he has taken.
how can I fix this?
The distinct method allows you to force the query to return distinct results, you can use it like this;
$users = DB::table('users')->groupBy('user_id')->distinct()->get();
If you want to take just the latest record, you should use "orderBy" to order your records, than pick a record with something like this :
$users = DB::table('users')->groupBy('user_id')->orderBy('created_at','desc')->distinct()->get();
You can look here for more detailed information about query builders in Laravel

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 count

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();

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