I have a SQL query and I want to rewrite it in eloquent syntaxe.
This is my query:
SELECT study_id FROM (
SELECT max(studies.end_date), studies.id as study_id
from workers
inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id) as maste
and there is my attempt to rebuild it with Eloquent:
$qr->select(DB::raw('study_id'))->from(function ($r) {
$r->selectRaw("max(studies.end_date), studies.id as study_id")
->from('workers')
->join('resumes', 'resumes.worker_id', '=', 'workers.id')
->join('studies', 'studies.resume_id', '=', 'resumes.id')
->where('resumes.title', 'main')->groupBy('workers.id');
});
Or generaly, How can I do a query from another query with Eloquent, like that :
SELECT id from(Select id,max(date) from b group by id)
There is my full query:
SELECT count(workers.id) as data ,titlesdiplomas.libelle as label
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id
inner join diplomas on diplomas.study_id=studies.id
inner join titlesdiplomas on titlesdiplomas.id=diplomas.titlesdiploma_id
where studies.id in(SELECT study_id FROM (
SELECT max(studies.end_date), studies.id as study_id
from workers
inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id) as maste)
and workers.id in (SELECT worker_id from career_histories where current=1 '
. $whereQuery . ' ) group by titlesdiplomas.libelle