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 !!!
Related
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
So with pure PHP and MySQL i can get away with filtering an query for example. $sql = select*from users; then i have the drop downs, country province and districts.
if a user selects nothing, click get report all users are displayed. If user select any from drop eg. country it should then narrow down the result based on country selected. user can also implement the same on all dropdowns.
So i am trying to archive the same with Laravel i will show you my code.
Already i have selected and joined tables and its bringing back my results. i have tried to add a where to the results if a dropdwon value is set but its then not giving me results like i expect
$results = DB::table('people')
->leftJoin('contacts', 'people.id', '=', 'contacts.person_id')
->leftJoin('provinces', 'contacts.province_id', '=', 'provinces.id')
->leftJoin('nationalities', 'people.nationality_id', '=', 'nationalities.id')
->leftJoin('districts', 'contacts.district_id', '=', 'districts.id')
->select('people.*', 'contacts.*', 'provinces.name AS province_name',
'nationalities.name AS nationality', 'districts.name AS district_name');
if (request()->has('nationality_id')) {
$nationality_id = request('nationality_id');
$results->where('people.nationality', '=', $nationality_id)->get();
}
return view('reports.index', compact('results', 'nationalities', 'provinces'));
i expect that if i select dropdown and like province it filters the collection with the selected value of province being selected.
you should edit this code:
if (request()->has('nationality_id')) {
$nationality_id = request('nationality_id');
$results = $results->where('people.nationality', '=', $nationality_id)->get();
}
To be this:
if ($nationality_id = request('nationality_id')) {
$results = $results->where('people.nationality', '=', $nationality_id);
}
$results = $results->get()
try this maybe it helps you
if (request()->has('nationality_id')) {
$nationality_id = request('nationality_id');
$results = DB::table('people')
->leftJoin('contacts', 'people.id', '=', 'contacts.person_id')
->leftJoin('provinces', 'contacts.province_id', '=', 'provinces.id')
->leftJoin('nationalities', 'people.nationality_id', '=', 'nationalities.id')
->leftJoin('districts', 'contacts.district_id', '=', 'districts.id')
->select('people.*', 'contacts.*', 'provinces.name AS province_name',
'nationalities.name AS nationality', 'districts.name AS district_name')
->where('people.nationality', '=', $nationality_id)->get();
}else{
$results = DB::table('people')
->leftJoin('contacts', 'people.id', '=', 'contacts.person_id')
->leftJoin('provinces', 'contacts.province_id', '=', 'provinces.id')
->leftJoin('nationalities', 'people.nationality_id', '=', 'nationalities.id')
->leftJoin('districts', 'contacts.district_id', '=', 'districts.id')
->select('people.*', 'contacts.*', 'provinces.name AS province_name',
'nationalities.name AS nationality', 'districts.name AS district_name')
->get();
}
You should rewrite $results variable:
//...
if (request()->has('nationality_id')) {
$nationality_id = request('nationality_id');
$results = $results->where('people.nationality', '=', $nationality_id)->get();
}
//...
Problem is with this line
$results->where('people.nationality', '=', $nationality_id)->get();
->get() method is returnin new Collection object, which you need to pass to another variable.
$results itslef is a Builderinstance.
Just change
$results->where('people.nationality', '=', $nationality_id)->get();
To
$results = $results->where('people.nationality', '=', $nationality_id)->get();
And you will get what you want.
I'm building a small application in Laravel 5.4 where I'm having following query:
$user = User::find(1);
$tasks = $user->tasks;
$tasks->count = $tasks->count();
$tasks->completedCount = $tasks->where('status', '=', 'Closed')->count();
$tasks->investorCount = $tasks->where('task_id', '=', 'Investor Targets')->count();
$tasks->investorCompletedCount = $tasks->where([
['task_id', '=', 'Investor Targets'],
['status', '=', 'Closed'],
])->get()->count();
$tasks->researchCount = $tasks->where('task_id', '=', 'Research Targets')->count();
$tasks->researchCompletedCount = $tasks->where([
['task_id', '=', 'Research Targets'],
['status', '=', 'Closed'],
])->get()->count();
dd($tasks);
I'm getting following error;
Missing argument 2 for Illuminate\Support\Collection::where()
In the line
$tasks->investorCompletedCount = $tasks->where([
['task_id', '=', 'Investor Targets'],
['status', '=', 'Closed'],
])->get()->count();
My syntax is also correct, I don't know from where this problem is coming. Help me out in this.
This code
$tasks->investorCompletedCount = $tasks->where([
['task_id', '=', 'Investor Targets'],
['status', '=', 'Closed'],
])->get()->count();
needs to be rewritten as
$tasks->investorCompletedCount = $tasks->where('task_id', 'Investor Targets')
->where('status', 'Closed')->count();
The problem is you are confusing the method where of an Eloquent query construction
(https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_where)
with the method whereof a Collection
(https://laravel.com/api/5.4/Illuminate/Support/Collection.html#method_where).
Actually you are trying to use a Collection as if it were a query builder, because you also try to perform a get.
You are mixing the Eloquent where with a Collection where.
Change your code to this:-
$tasks->investorCompletedCount = $tasks->where('task_id','Investor Targets')
->where('status', 'Closed')->count();
You can use like this
$tasks->investorCompletedCount = $tasks
->where(['task_id' => 'Investor Targets','status' => 'Closed'])
->get()
->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();
I have a question about inner joins with multiple on values.
I did build my code like this in laravel.
public function scopeShops($query) {
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
// $join->on('kg_shops.active', '=', "1"); // WRONG
// EDITED ON 28-04-2014
$join->on('kg_shops.active', '=', DB::raw("1"));
});
}
Only problem is, it gives this outcome:
Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select `kg_feeds`.* from `kg_feeds` inner join `kg_shops` on `kg_shops`.`id` = `kg_
feeds`.`shop_id` and `kg_shops`.`active` = `1`) (Bindings: array ( ))
As you can see, the multiple conditions in the join go fine, but it thinks the 1 is a column instead of a string. Is this even possible, or do I have to fix it in the where.
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
})
->select('required column names')
->where('kg_shops.active', 1)
->get();
You can see the following code to solved the problem
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
$join->where('kg_shops.active','=', 1);
});
Or another way to solved it
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
$join->on('kg_shops.active','=', DB::raw('1'));
});
//You may use this example. Might be help you...
$user = User::select("users.*","items.id as itemId","jobs.id as jobId")
->join("items","items.user_id","=","users.id")
->join("jobs",function($join){
$join->on("jobs.user_id","=","users.id")
->on("jobs.item_id","=","items.id");
})
->get();
print_r($user);
Because you did it in such a way that it thinks both are join conditions in your code given below:
public function scopeShops($query) {
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
$join->on('kg_shops.active', '=', "1");
});
}
So,you should remove the second line:
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
});
Now, you should add a where clause and it should be like this:
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id')->where('kg_shops.active', 1);
})->get();
You can simply add multiple conditions by adding them as where() inside the join closure
->leftJoin('table2 AS b', function($join){
$join->on('a.field1', '=', 'b.field2')
->where('b.field3', '=', true)
->where('b.field4', '=', '1');
})
More with where in (list_of_items):
$linkIds = $user->links()->pluck('id')->toArray();
$tags = Tag::query()
->join('link_tag', function (JoinClause $join) use ($linkIds) {
$joinClause = $join->on('tags.id', '=', 'link_tag.tag_id');
$joinClause->on('link_tag.link_id', 'in', $linkIds ?: [-1], 'and', true);
})
->groupBy('link_tag.tag_id')
->get();
return $tags;
Hope it helpful ;)
This is not politically correct but works
->leftJoin("players as p","n.item_id", "=", DB::raw("p.id_player and n.type='player'"))