Laravel Eloquent how to query with multiple 'froms'? - laravel

How do I convert a query like this to eloquent:
select table1.column, table2.column, table3.column from table1, table2, table3

You can use achieve this by
DB::query()
->from('table1, table2, table3')
->select('table1.column', 'table2.column', 'table3.column')
->get()

You can use fromRaw():
DB::query()
->select('table1.column', 'table2.column', 'table3.column')
->fromRaw('table1, table2, table3')
->get();

Related

Crudbooster laravel - How to make different column relationship?

I have two table 1. table_a 2. table_b
And all table have company_id. I want to make relationship among this two table using same company id . Like
select * from table_a left join table_b on table_a.company_id = table_b.company_id;
Please help me out if its really possible. Thank you in advance.
You can use this:
$data = DB::table('table_a')->leftJoin('table_b', 'table_a.company_id', '=', 'table_b.company_id')->get();
Or raw:
$users = DB::table('table_a')
->select(DB::raw('query here'))
->get();
More on:
https://laravel.com/docs/5.7/queries

Sub-queries in Laravel

I have a query in MySQL
SELECT * FROM group_recordings WHERE id IN ( SELECT MAX(id) FROM group_recordings GROUP BY time_span )
there is a sub-query in "IN" operator, I want to convert this query in laravel eloquent, I have GroupRecording Model. Anyone help me with that
I think the Laravel style of such query will be,
GroupRecording::whereIn('id', function($query) {
$query->selectRaw('MAX(id)')
->from('group_recordings')
->groupBy('time_span');
})
->where('time_span', '>', $recording->time_span)
->get();

Laravel Left Join Query

I am using laravel 5.3 and I have some left join query with error in laravel query method.
This is my normal query
SELECT bran.branchName,sch.schoolName From m_schoolbranch bran
LEFT JOIN m_students stu ON stu.schoolNo=bran.schoolNo AND stu.branchNo=bran.branchNo
LEFT JOIN m_school sch ON sch.schoolNo=stu.schoolNo where stu.userNo='0000000001';
And this is my new laravel Query
DB::table('m_schoolbranch')
->join('m_students', 'm_schoolbranch.schoolNo', '=', 'm_students.schoolNo')
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();
In these query I need to match two column in table m_students so I put like this
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
But i show error...
Tables in the query need to have unique names, otherwise the DB has no way of knowing which m_schoolbranch should be used when evaluating m_schoolbranch.schoolNo.
You could use unique table aliases in your join statements but I recommend using multiple conditions on the join. Just like you use in your original SQL query. See here: https://stackoverflow.com/a/20732468/4437888
DB::table('m_schoolbranch')
->join('m_students', function($join)
{
$join->on('m_schoolbranch.schoolNo', '=', 'm_students.schoolNo');
$join->on('m_schoolbranch.branchNo', '=', 'm_students.branchNo');
})
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();

laravel 4.2 and join function

$options = table::join('table1', function($join) {
$join->on('table1.table_id','=','table.id');
$join->take(1);
})->get();
Is there a way to do this, i get error on take function.
the table and table1 has one to many relation, and i'd like to have on join just the first record of table1.
Thanks
Vincenzo

eloquent where not in query?

I'm trying to build the following sql query with eloquent. The query gives me all records from table_a which are in the list of ids and do not appear in table_b.
select * from table_a
where id in (1,2,3)
and id not in
(select tablea_id from table_b
where tablea_id in (1,2,3))
So how do I do it in eloquent ? I want to avoid using a raw query.
//does not work
TableA::whereIn('id',$ids)
->whereNotIn('id', TableB::select('tabla_id')->whereIn($ids));
To run a subquery you have to pass a closure:
TableA::whereIn('id',$ids)
->whereNotIn('id', function($q){
$q->select('tabla_id')
->from('tableb');
// more where conditions
})
->get();

Resources