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

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

Related

Use query 2 in query 1 to get all data inside one query

I have 3 tables :
*hinteractions (id, name...)
*effects (id, name...)
*hinteractions_has_effects (hinteractons_id, effects_id)
I might have one to many effects in a hinteractions et one hinteraction might have one to many effects, that's why I have this join table.
I have this Eloquent query :
Query 1 :
$informations_plante = DB::table('herbs')
->select('herbs.name as hname', 'herbs.sciname', 'herbs.id as herbid','hinteractions.id as hinteractionid','hinteractions.note as hinteractionnote','hinteractions.force_id','targets.name as targetname', 'forces.name as force_name')
->leftJoin('hinteractions', 'herbs.id', '=', 'herb_id')
->leftJoin('forces', 'forces.id', '=', 'force_id')
->leftJoin('targets', 'targets.id', '=', 'hinteractions.target_id')->where('herbs.id', $id)
//I would like to use the query 2 here to select effects.name with hinteraction.id used in this query to get effects' name in this query (I might have one to many).
->get();
Query 2 :
$hinteractions_has_effects = DB::table('hinteraction_has_effects')
->select(DB::raw('effect_id, hinteraction_id','effects.name'))
->where('hinteraction_has_effects', '=', hinteraction.id (from the query 1))
->get();
With query 1, I retrieve some informations like hinteractions_id.
I would like to use those hinteractions_id and use them in the query 2.
The best way will be to merge both queries (1 and 2) to get only one Eloquent query.
Do you have any idea ?
Try below query and let me know if this is what you're looking for -
$informations_plante = DB::table('herbs')
->select('herbs.name as hname', 'herbs.sciname', 'herbs.id as herbid','hinteractions.id as hinteractionid','hinteractions.note as hinteractionnote','hinteractions.force_id','targets.name as targetname', 'forces.name as force_name', 'effects.id as effects_id', 'effects.name as effects_name')
->leftJoin('hinteractions', 'herbs.id', '=', 'herb_id')
->leftJoin('forces', 'forces.id', '=', 'force_id')
->leftJoin('targets', 'targets.id', '=', 'hinteractions.target_id')->where('herbs.id', $id)
//added below lines
->leftJoin('hinteraction_has_effects', 'hinteraction_has_effects.hinteractons_id', '=', 'hinteractions.id')
->leftJoin('effects', 'hinteraction_has_effects.effects_id', '=', 'effects.id')
->get();
HTH!

How to convert mysql query to laravel query builder

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

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')

How to determine columns that will be returned in the result when using `paginate()` method in Laravel?

I have 2 tables: users and articles. To fetch all columns from the articles table and only user_name column from the users table, I use this code:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->get(array('articles.*', 'users.user_name'));
and it works fine, but when I use paginate() method like this:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->paginate(10);
it fetches all columns from both tables, which I don't want. My question is: How can I select columns that will be returned in the result if I use paginate() method in Laravel framework?
The select function does this.
$articles = Article::join('users', 'articles.user_id', '=', users.user_id')
->select('articles.*', 'users.user_name')
->paginate(10);

Laravel 4: Query builder retrieve join table data

In Laravel, we could join 2 tables with this:
DB::table("users")->join('moreusers', 'users.id', '=', 'moreusers.user_id')->get();
And retrieve data with this:
$querydata[$i]->email
If both tables contain the same column name, how can I specifically retrieve it from first and second table?
Thanks.
Use select() method:
DB::table("users")
->join('moreusers', 'users.id', '=', 'moreusers.user_id')
->select('users.email as user_email', 'moreusers.email as other_email')
->get();

Categories