I have a problem to make a query in Laravel
My query in POSTGRESQL is:
SELECT con.id, con.vehiculo_id, con.cre, ve.chofer_id, ve.placa
FROM conciliaciones as con
JOIN vehiculos as ve
on con.vehiculo_id = ve.id or con.vehiculo_id IS NULL or con.vehiculo_id IS NOT NULL;
The problem with the query is that the plate is repeated when there should only be 1 single row with the data plate the rest is null
I have the following code, but it does not work:
$conciliacion = DB::table('conciliaciones')
->join('vehiculos','conciliaciones.vehiculo_id','=','vehiculos.id')
->select('conciliaciones.*','vehiculos.placa')
->whereNull('conciliaciones.vehiculo_id')
->whereNotNull('conciliaciones.vehiculo_id')
->get();
How can I make the query?
In the query, select other columns but is Similarly that I need in Laravel
thanks
my solution xD
$conciliacion = DB::table('conciliaciones')
->leftJoin('vehiculos','vehiculos.id', '=', 'conciliaciones.vehiculo_id')
->select('conciliaciones.*','vehiculos.placa')
->orderBy('conciliaciones.id')
->get();
Related
I'm new to Laravel, I wanted to make a union with the DB and it didn't work for me, I tried it in SQL and it worked fine, I don't know what I'm doing wrong?
Would you be kind enough to help me with this dilemma, thanks
here is the SQL query:
SELECT SUM(materias.Credito) FROM historico_notas INNER JOIN materias ON materias.id = historico_notas.materias_id INNER JOIN estudiante_seccion on estudiante_seccion.id = historico_notas.estudiante_seccion_id WHERE historico_notas.nota >= 10 and estudiante_seccion.id_persona = 627;
This is the generated query SQL:
here in my attempt in laravel:
$prueba = DB::table('historico_notas')
->join('materias','materias.id', '=',' historico_notas.materias_id ' )
->join('estudiante_seccion',' estudiante_seccion.id','=','historico_notas.estudiante_seccion_id' )
->where('historico_notas.nota','>=',10 )
->where(' estudiante_seccion.id_persona','=',$persona_alumno)
->sum('materias.Credito');
This is the generated query and error.
My dilemma is that I want my sql query to Laravel and something gives me an error and I don't know what error I have
the SQL query works perfectly for me, but when I try to go to Laravel it gives me an error,
what i show is my attempt to pass the sql query to laravel and what i want is my sql query to laravel
How would the syntax be properly?
I just converted your SQL into this.
$query = DB::table('historico_notas')
->join('materias', 'materias.id', '=', 'historico_notas.materias_id')
->join('estudiante_seccion', 'estudiante_seccion.id', '=', 'historico_notas.estudiante_seccion_id')
->where('historico_notas.nota', '>=', 10)
->where('estudiante_seccion.id_persona', '=', 627)
->sum('materias.Credito');
The error gets an unknown column "historico_notas.nota", you can check it in the table if it exists
I'm trying to use Laravel Query Builder to join 2 tables.
Here's my query for joining 2 tables
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->get();
The code let me join the table properly however this results to multiple duplication. Take a look at the result passed in select tag
Here's the schema I have
Instructor Table
Instructor Courses Table
Basically what I want to achieve is to display a record without duplication. Is there a better approach in joining multiple table like this one? Thank you
You should use groupBy for instructors.id
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->groupBy('instructors.id') //add a line
->get();
Try to use the ->distinct() function in laravel to prevent duplicates in selecting data.
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->groupBy('instructors.id')
->distinct()
->get();
I have this code:
$abcd= DB::table('control')
->leftJoin('proces', 'proces.id_control', '=', 'control.id_control')
->select('control.id_risk', DB::raw("group_concat(proces.id_procedur) as prc"))
->where('control.id_theme', '=', $id)
->whereExists(function ($query) {
$query->select('id_control')
->from('proces') ->where('control.id_control','proces.id_control');
})
->groupBy('control.id_risk')
->get();
This query work perfectly, when i wrote it in phpmyadmin, but when I try to execute it in app query return empty array. Where is problem
UPDATE
After i check how looks query from laravel there is one wrong
select `control`.`id_risk`, group_concat(proces.id_procedur) as prc from `control` left join `proces` on `proces`.`id_control` = `control`.`id_control` where `control`.`id_theme` = ? and exists (select `id_control` from `proces` where `control`.`id_control` = ?) group by `control`.`id_control`
First question mark is corrent beacue this $id(by the way variable is passing good value) but the second question mark should not be there.
I'm using the following code to create a query with a join.
$query = rs_vehicles::select(
'rs_vehicles.id',
'rs_vehicles.type',
'rs_vehicles.make',
'rs_vehicles.model',
'rs_vehicles.year',
'rs_vehicles.status',
'rs_vehicle_regs.registration'
);
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id')
->where('rs_vehicle_regs.registration_date', '=',
DB::raw("(select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)"));
});
$rsGrid = $query->get();
This produces the following sql statement:
select rs_vehicles.id, rs_vehicles.type, rs_vehicles.make,
s_vehicles.model, rs_vehicles.year, rs_vehicles.status,
s_vehicle_regs.registration from rs_vehicles
left join rs_vehicle_regs
on rs_vehicle_regs.rs_vehicles_id = rs_vehicles.id
and rs_vehicle_regs.registration_date = (select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)
The SQL statement generated by Laravel runs perfect when I execute it in MySQL Workbench and it brings back the expected values for ALL fields. However when I execute the code in Laravel although the script runs perfectly with no errors and brings back almost all the required data the 'rs_vehicle_regs.registration' field which is in the joined table comes back as null. I've spent ages trying to figure this out and I am getting nowhere. Anybody any idea why this wont work in Laravel? I'm at my wits end.
I have tried changing get() to toSql() and I get the following
select `rs_vehicles`.`id`, `rs_vehicles`.`type`, `rs_vehicles`.`make`, `rs_vehicles`.`model`, `rs_vehicles`.`year`, `rs_vehicles`.`vehicle_status`, `vh_type`.`display_text` as `type_text`, `vh_status`.`display_text` as `vehicle_status_text`, `rs_vehicle_regs`.`registration` as `registration` from `rs_vehicles` left join `app_params` as `vh_type` on `rs_vehicles`.`type` = `vh_type`.`list_value` and `vh_type`.`param_type` = ? left join `app_params` as `vh_status` on `rs_vehicles`.`vehicle_status` = `vh_status`.`list_value` and `vh_status`.`param_type` = ? left join `rs_vehicle_regs` on `rs_vehicle_regs`.`rs_vehicles_id` = `rs_vehicles`.`id` where `rs_vehicles`.`status` = ?
I have also tried the following:
$query ->leftJoin('rs_vehicle_regs', function($join){
$join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_employees.rs_vehicles_id')
->where('rs_vehicle_regs.registration_date', '=', 'max(registration_date)');
});
This produces the exact same result as the original query where the object as the field but the values are null.
Then I tried this:
$query ->leftJoin('rs_vehicle_regs', 'rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id');
This join works and brings back the data I am looking for however if the join table has more than one entry related to the parent table as can happen I get all records returned and I only want one record were max(registration_date). This has me totally stumped.
Is there any way to convert following query to laravel query builder?
select `employee_id`
from `otc_employee_qualifications`
where `emp_qualifctn_type` IN ('29','27')
group by `employee_id`
having count(Distinct `emp_qualifctn_type`) = 2
Try as below :
$users = DB::table('otc_employee_qualifications')
->select('employee_id')
->whereIn('emp_qualifctn_type', [27,29])
->groupBy('employee_id')
->having(DB::raw("count(Distinct emp_qualifctn_type)"), '=', 2)
->get();
Answer:
DB::select('employee_id')
->from('otc_employee_qualifications')
->whereIn('emp_qualifctn_type', ('29', '27'))
->groupBy('employee_id')
->having(DB::raw('count(Distinct emp_qualifctn_type)'), '=', 2)
->get();
You can convert SQL query into laravel eloquent query by using bellow website.
This will convert SQL query to laravel eloquent base query
Convert SQL query to Eloquent base query