How can I get the value from the DB query? - php

Code below:
$data = DB::table('information')
->select(DB::raw('SUM(working) as working'))
->where('name', '=', 'ismael')
->get();
error_log($data);
it print: [{"working":"23"}]
how can I get the 23? without the "working"

$data = DB::table('information')
->select(DB::raw('SUM(working) as working'))
->where('name', '=', 'ismael')
->first()->working;

Use method sum:
DB::table('information')->where('name', '=', 'ismael')->sum('working')
Or call the attribute:
DB::table('information')->where('name', '=', 'ismael')->select(DB::raw('SUM(working) as working'))->first()->working

Related

Laravel 5.4: How to order by count(column) in eloquent?

I am trying to display the names of assignee arranged by the most tickets assigned_to.
$accesses = Access::where('state','=','Assigned');
$all = Report::where('state', '=', 'Assigned')
->union($accesses)
->orderBy('count(assigned_to)') //THIS IS WRONG
->get();
you have to use DB::raw to get it
$all = Report::where('state', '=', 'Assigned')
->select(DB::raw('count(reports.assigned_to) as assigned_to'))
->union($accesses)
->orderBy('assigned_to','DESC')
->get();
Try like this
$accesses = Access::where('state','=','Assigned');
$all = Report::where('state', '=', 'Assigned')
->union($accesses)
->orderBy(DB::raw('count(assigned_to)'),'DESC')
->get();
Try this:-
$all = Report::where('state', '=', 'Assigned')
->select(DB::raw('count(reports.assigned_to) as assigned_to'))
->union($accesses)
->orderBy('assignedto','DESC')
->get();
I think you can try this :
$all = Report::where('state', '=', 'Assigned')
->select(DB::raw('count(reports.assigned_to) as assigned_to'),DB::raw('count(access.assigned_to) as assigned_to_access'))
->leftjoin('access','report.access.id','=','access.id')
->union($accesses)
->orderBy('assigned_to','DESC')
->orderBy('assigned_to_access','DESC')
->get();
Hope this help for you !!!

Using 'LIKE' in laravel

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/

Laravel 5.2 DB Query

I am trying to use DB to build a query like so:
Get all Distributors belonging to the User with their Accounts that have Notes.
I cant wrap my head around that query. Here is what I had that works in that it gets spits out the info I need in one array but 2 issues: the note name overwrites the account name since they are both called that, and I cant pass in a $user-> to replace the 2
Working (kind of)
$user = Auth::user();
$accounts = DB::table('distributors')
->join('accounts', function ($join) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', 2);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name', 'notes.*')
->get();
What I need
$user = Auth::user();
$accounts = DB::table('distributors')
->join('accounts', function ($join, $user) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', $user->id);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name (this is overwritten. can I change it to come out account_name so it doesnt?)', 'notes.*')
->get();
Try this for account name:
->select('accounts.name as custom_account_name')
Hope this helps.
Try this:
$user = Auth::user();
DB::table('distributors')
->join('accounts', function ($join) use ($user) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', $user->id);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name as account_name', 'notes.*')
->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();

Categories