Laravel Eloquent select use group by - laravel

When I query the database I get the results I want.
$data['jobtypes'] = User::Select('jobtype_search', DB::raw('count(jobtype_search) as jobs'))->
Where('jobtype_search', '<>', '')
->GroupBy('jobtype_search')
->get();
However, when I do not want to keep calling the database for every query. Instead I call on the database once to get the chunk of data I need and then query off that.
$user = User::whereBetween('created_at', [$start_date, $end_date])->get();
When I try
$data['jobtype_search'] =$user->Select('jobtype_search', DB::raw('count(jobtype_search) as jobs'))->
Where('jobtype_search', '<>', '')
->GroupBy('jobtype_search')
->get();
I get a method select not found error.
What should I change so my query works?

The $user variable in your example is the result set (a Laravel collection) of the query, but you're trying to use it as a query.
This would work (another query):
$data['jobtype_search'] = User::whereBetween('created_at', [$start_date, $end_date])
->Select('jobtype_search', DB::raw('count(jobtype_search) as jobs'))
->Where('jobtype_search', '<>', '')
->GroupBy('jobtype_search')
->get();
Or if you want to do it on the result set, you could do it with the collection methods on the result set:
$data['jobtype_search'] = $user
->filter(function ($item) {
return $item->jobtype_search !== '';
})
->groupBy('jobtype_search')
->map(function ($item, $jobTypeSearch) {
return [
'jobtype_search' => $jobTypeSearch,
'jobs' => $item->count(),
];
})

Related

Laravel OrWhere having two conditions is not so obvious

Why in Eloquent a simple query as this one is not working?
$matches = DB::table("matches")
->where(["team_a"=> $request->tid, "match_tipo"=>3])
->orWhere(["team_a"=> $request->tid, "match_tipo"=>3])
->first();
According to with other examples here on Stackoverflow, I should use a query in Where like:
$matches = DB::table("matches")
->where(["match_tipo"=>3])
->where(function($query, $request) {
$query->where('team_h',$request->tid)
->orWhere('team_a',$request->tid);
})
->first();
But I should pass a second parameter ($request).
How is the simplest way to do this query?
You can make this as like this.
->where(["match_tipo"=>3]) this will be ->where("match_tipo",3) and use($tid) after query.
$tid = $request->tid;
$matches = DB::table("matches")
->where("match_tipo",3)
->where(function($query) use($tid) {
$query->where('team_h',$tid)
->orWhere('team_a',$tid);
})
->get();
while using the Callback or Closure as the where function Parameter you will only the one argument which is Current Object
Methods Can used to Pass the $request
Method One
$matches = DB::table("matches")
->where(["match_tipo"=>3])
->where(function($query) use ($request) {
$query->where('team_h',$request->tid)
->orWhere('team_a',$request->tid);
})
->first();
Method Two
$matches = DB::table("matches")
->where('match_tipo','=',3)
->when($request->tid,function($query) use ($request) {
return $query->where('team_h',$request->tid)
->orWhere('team_a',$request->tid);
})
->first();
Try this query :
$matches = DB::table("matches")
->where('match_tipo',3)
->whereRaw('team_a = "'.$request->tid.'" OR team_h = "'.$request->tid.'"')
->first();

DB::raw convert to query builder

Can you help me to convert the Raw part of my query to use query builder?
I am stuck at joining it all together:
$profile = UserProfiles::select('id')->where('alias', $profileAlias)->first();
$dbRawMessagesCount = '
(SELECT COUNT(pm.id)
FROM profile_messages pm
WHERE pm.to_profile_id='.$profile->id.'
AND pm.from_profile_id=profile_friend.id
AND pm.is_read=0) AS messages_count
';
$friends = ProfileFriend::select('profile_friend.*', DB::raw($dbRawMessagesCount))
->with('friendProfile')
->whereHas('ownerProfile', function ($query) use ($profile) {
return $query->where('id', $profile->id);
})
->orderBy('messages_count')
->paginate();
You can rewrite this into one query if ProfileFriend has a relation already set up to ProfileMessages using withCount() in the query.
$friends = ProfileFriend::with('friendProfile')
->withCount(['profileMessages' => function($q) use($profile){
$q->where('to_profile_id', $profile->id)->where('is_read', 0);
// No longer need 'from_profile_id' as it is already querying the relationship
}])
->whereHas('ownerProfile', function ($query) use ($profile) {
return $query->where('id', $profile->id);
})
->paginate();
Now if you dd($friends->first()) you will notice it has a field called profileMessages_count that gives you a count of what I'm assuming is unread messages.

Laravel eloquent using loop

I am using eloquent query to filter data. My query is like this and it is working perfectly for now.
$users = User::where('username','LIKE','%'.request('username').'%')
->where('first_name','LIKE','%'.request('first_name').'%')
->where('last_name','LIKE','%'.request('last_name').'%')
->where('gender','LIKE','%'.request('gender').'%')
->where('password','LIKE','%'.request('password').'%')
->SimplePaginate(15);
My request data was like this.
However I need to update this query for dynamic fields. There can be different fields. What I did was to send the request in an associative array. So, my request data turned into like this,
What I intend to do is to put all request data in a search array. Then use a loop to run like the above query. How can I do that?
P.S. I checked in the forum and found some similar questions. Some of them are outdated. Others didn't solve my problem.
If I understand you right you can do like this:
$search = request('search', []);
$users = User::query();
foreach($search as $field=>$value)
{
$users = $users->where($field,'LIKE','%'.$value.'%');
}
$users = $users->SimplePaginate(15);
You can try this.
$search = $request->get('search');
User::where(function ($q) use ($search){
foreach ($search as $key=>$value) {
if($value != null ){
$q->where($key, 'like', "%{$value}%");
}
}
})->get();
The where() clause can also accept an array. With that in mind, you could map over your search parameters and convert it to the format you want and then feed it into the query.
For example:
$search = [
'first_name' => 'John',
'last_name' => 'Smith'
];
$filters = collect($search)->map(function($value, $key) {
return [$key, 'LIKE', "%{$value}%"];
})->values()->toArray();
return User::where($filters)->SimplePaginate(15);

Parameters Group on eloquent query

I have little problem with my search query at the moment. When i run a search query with only the parameter "comite_id" it's working but when i want to run the search query with "comite_id" and the whereBetween with "date_min" and "date_max" the result is not right , it's seems the result don't care about the whereBetween clause because i don't get the right date range.
Someone knows where i'm doing wrong? thanks a lot in advance
Here my controller :
public function getRetrocession(Request $request){
$comites = Structure::where('type_structure_id' , '=' ,'3')->pluck('nom_structure' , 'id');
$search = $request->input('comite_id');
$dt_min = $request->input('dt_min');
$dt_max = $request->input('dt_max');
if ($search) {
$query = Retrocession::where('comite_id', '=', $search)
->orWhere(function ($q) use($search , $dt_min , $dt_max) {
$q->where('comite_id', '=', $search)
->whereBetween('dt_retrocession', [ $dt_min , $dt_max]);
});
}else {
$query = Retrocession::select();
}
$retrocessions = $query->orderBy('dt_retrocession', 'DESC')->paginate(10)
->appends(['recherche' => $search]);
return view('retrocession/index' , compact('retrocessions' , 'comites'));
}
Because you are using orWhere() it always call both where() and orWhere(); thus including the result of where() and orWhere(). You can try this way
$query = Retrocession::where('comite_id', $search)
->when($dt_min && $dt_max, function($q) use ($dt_min, $dt_max) {
$q->whereBetween('dt_retrocession', [$dt_min, $dt_max]);
});
So you always run the first where, and only include whereBetween() WHEN $dt_min and $dt_max are both true.
Bonne chance Mathieu.

Eloquent / Laravel - Putting a WHERE Clause on a Reference Table With Chained Relationships

I have the following relationship functions in my Job model:
public function resourceTypes(){
return $this->belongsToMany('ResourceType', 'job_requests');
}
public function resources(){
return $this->belongsToMany('Resource', 'jobs_resources')->withPivot('flow_type', 'resource_type_id');
}
I am able to get an object with data from both of the above relationships using:
$job = Job::findorfail($projectId);
$result = $job->with('resources.resourceTypes')->get();
I would like to put a where clause on the jobs_resources pivot table - specifically on the column flow_type.
How would I do this?
Try something like this:
$job = Job::with('resources' => function($q) {
$q->with('resourceTypes')->where('flow_type',2);
})->findorfail($projectId);
In above you will get only those resources with flow_type = 2
I ended up using the following statement:
Job::with(['resources' => function ($query){
$query->wherePivot('flow_type', '=', '1' );
}, 'resources.resourceTypes'])->where('id', $projectId)->firstOrFail();
$result = DB::table('job')
->join('job_resources', 'job.id', '=', 'job_resources.job_id')
->join('job_requests', 'job_resources.request_id', '=', 'job_requests.id')
->where('job_resources.flow_type', '=', CONDITION)
->get();
Your table data is not clear from your input, but this method (query builder) should work

Resources