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();
Related
I made this code from a project I'm currently developing at work. The problem I'm having is that this code:
php
public function getAllEstaciones(Request $request){
$estaciones = Estacion::select('IdEstacion', 'proyectoplanning.NombreProyectoPlanning', 'modeloequipo.NombreModeloEquipo',
'tipoinstalacion.NombreTipoInstalacion', 'estacion.IdEstadoGeneral', 'estadogeneral.NombreEstadoGeneral',
'mediotransmision.NombreMedioTransmision', 'propietarioestacion.NombrePropietarioEstacion', 'centropoblado.NombreCentroPoblado',
'distrito.NombreDistrito', 'provincia.NombreProvincia', 'departamento.NombreDepartamento', 'tiposervicio.NombreTipoServicio', 'estacion.IdBtsEstacion',
'btsestacion.Direccion', 'CodigoPresupuestalPlanning', 'CodigoUnicoPlanning', 'Prioridad', 'LatitudPlanning', 'LongitudPlanning',
'distrito.NombreDistrito', 'provincia.NombreProvincia', 'departamento.NombreDepartamento', 'tiposervicio.NombreTipoServicio',
'estacion.IdBtsEstacion', 'btsestacion.Direccion', 'CodigoPresupuestalPlanning', 'CodigoUnicoPlanning', 'Prioridad',
'LatitudPlanning', 'LongitudPlanning')
->join('proyectoplanning', 'proyectoplanning.IdProyectoPlanning', '=', 'estacion.IdProyectoPlanning')
->join('modeloequipo', 'modeloequipo.IdModeloEquipo', '=', 'estacion.IdModeloEquipo')
->join('tipoinstalacion', 'tipoinstalacion.IdTipoInstalacion', '=', 'estacion.IdTipoInstalacion')
->join('estadogeneral', 'estadogeneral.IdEstadoGeneral', '=', 'estacion.IdEstadoGeneral')
->join('mediotransmision', 'mediotransmision.IdMedioTransmision', '=', 'estacion.IdMedioTransmision')
->join('propietarioestacion', 'propietarioestacion.IdPropietarioEstacion', '=', 'estacion.IdPropietarioEstacion')
->join('centropoblado', 'centropoblado.IdCentroPoblado', '=', 'estacion.IdCentroPoblado')
->join('distrito', 'distrito.IdDistrito', '=', 'centropoblado.IdDistrito')
->join('provincia', 'provincia.IdProvincia', '=', 'distrito.IdProvincia')
->join('departamento', 'departamento.IdDepartamento', '=', 'provincia.IdDepartamento')
->join('tiposervicio', 'tiposervicio.IdTipoServicio', '=', 'estacion.IdTipoServicio')
->join('btsestacion', 'btsestacion.IdBtsEstacion', '=', 'estacion.IdBtsEstacion')
->get();
return response()->json($estaciones);
}
This is a query I built using the Eloquent module form Laravel. It's only returning one row from the table "Estaciones". I tried doing $estaciones = DB::table("estacion") with no success. I'd be grateful if you guys could give me some insight.
Hello I am having trouble getting values in my laravel code. See here and notice ->where('created_at', 'LIKE', '%'.'2017-04-12'.'%')
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
//->where($request->filters)//will act as a searchmap
->where('created_at', 'LIKE', '%'.'2017-04-12'.'%')
->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
My question is what's the proper way of using 'LIKE' because I can't get any values from it. The reason I am using it is that because that column has other string to it. See this picture.
Is it possible to only read/or find '2017-04-12' in that column(I am doing this for testing purposes).
Since you're using Eloquent, I'd recommend you to use whereDate() instead:
$date = Carbon::parse('2017-04-12')->toDateString();
....
->whereDate('created_at', $date)
If you don't have Carbon object, you can just use string:
->whereDate('created_at', '2017-04-12')
You can use it like:
Method1 using Raw query:
$q->where(DB::raw("DATE(created_at) = '".date('Y-m-d')."'"));
Method 2 by whereDate:
$q->whereDate('created_at', '=', date('Y-m-d'));
For more details visit:
http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/
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();
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();
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();