How to convert mysql query to laravel query builder - php

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

Related

Query in Laravel 5.7 and PostgreSQL

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();

How do I select multiple columns across tables with Laravel query builder?

I have a Laravel Eloquent query where I am trying to select multiple columns from a MySQL table.
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->get();
It looks like the select() function takes three arguments: query, bindings and useReadPdo. The above query gives me an error:
{"error":true,"message":"Type error: Argument 1 passed to Illuminate\\Database\\Connection::prepareBindings() must be of the type array, string given" }
How do I write a select with Laravel query builder for the above columns?
I am structuring the query in this way, because I am looking to have a join across another table like so:
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->leftJoin('myothertable', function($join){
$join->on('mytable.id', '=', 'myothertable.id');
})
->get();
How do I use the select function to grab multiple columns across tables with Eloquent query builder?
How do I write a select with Laravel query builder for the above columns?
You can do:
$data = DB::table('mytable')
->join('myothertable', 'mytable.id', '=', 'myothertable.mytable_id')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id'
)
->get();
You can read the documentations here

Join along with sum and order by in Eloquent

I'm trying to perform the following SQL query using Eloquent:
select s.username, s.link, s.followers, sum(h.count)
from users s, views h where h.username = s.username order by h.timestamp
I'm aware of the join(), sum() and select() methods, however, how can I perform the sum() and select() together?
To use a SUM() you will have to use DB::raw. Here is an example that should make your query work:
DB::table('users as s')
->select(DB::raw('s.username, s.link, s.followers, sum(h.count)'))
->join('views as h', 'h.username', '=', 's.username')
->orderBy('h.timestamp')
->get();

Laravel Query Builder WHERE NOT IN with multiple WHERE clause

I have following query,
SELECT * FROM `users` WHERE approved='1'
and users.id NOT IN (SELECT donations.user_id FROM donations where month='1'
and year='2016');
I have followed Laravel Query Builder WHERE NOT IN
and converted my query like this,
$users = DB::table('users')->where('approved','=',1)->whereNotIn('id', function($q){
$q->select('user_id')->from('donations')->where('month','=','$month')->where('year','=','$year');
})->get();
But its not working, can anybody help me out where i am making mistake.
how can I convert it into Laravel query builder format?
Thanks.
You shouldn't put your $month and $year variables in quotes.
Replace
->where('month','=','$month')->where('year','=','$year')
with
->where('month','=',$month)->where('year','=',$year)

Laravel Query Builder - sum() method issue

I'm new to laravel and I have some issues with the query builder.
The query I would like to build is this one:
SELECT SUM(transactions.amount)
FROM transactions
JOIN categories
ON transactions.category_id == categories.id
WHERE categories.kind == "1"
I tried building this but it isn't working and I can't figure out where I am wrong.
$purchases = DB::table('transactions')->sum('transactions.amount')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->select('transactions.amount')
->get();
I would like to get all the transactions that have the attribute "kind" equal to 1 and save it in a variable.
Here's the db structure:
transactions(id, name, amount, category_id)
categories(id, name, kind)
You don't need to use select() or get() when using the aggregate method as sum:
$purchases = DB::table('transactions')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->sum('transactions.amount');
Read more: http://laravel.com/docs/5.0/queries#aggregates
If one needs to select SUM of a column along with a normal selection of other columns, you can sum select that column using DB::raw method:
DB::table('table_name')
->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1'))
->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');
return $data;
You may add any condition to your record.
Thanks
MyModel::where('user_id', $_some_id)->sum('amount')

Categories