SELECT * FROM (SELECT ..) in Eloquent (Laravel4) - laravel

How to do this query: SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id with Eloquent in Laravel4?
Thanks in advance.

I just looked it up but couldn't find a way to do it.
What you can is to just create a normal Eloquent query and then sort it by PHP.
$query = MyModel::orderBy('id', 'desc')->limit(3)->get();
$query->sortBy(function($object) {
return $object->id;
});
dd($query->toArray());

Raw: DB::select('SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id');
Why using Eloquent all the time? It has so many limitations. You have 3 options when querying a database. Eloquent, Query Builder and Raw.

Related

GraphQL Where Select Sub Query

These day, i have studied GraphQL with Laravel framework, Lighthouse Library.
I have tried to do kind of SELECT Query.
As a result, I wonder GraphQL can select below SQL Query
SELECT * FROM TABLE_A WHERE type=1 AND chart_id=(SELECT id FROM TABLE_B WHERE phone='0000~~')
I expect, Client first get result from this query.
SELECT id FROM TABLE_B WHERE phone='0000~~'
And then Do Second query, i think i can get a result.
But i wonder I can get result from 1 request. Thanks.
You can try following
$phoneNumber = '0000~~';
$data = DB::table('tableA')->where('type',1)
->whereIn('chart_id',function($query) use ($phoneNumber) {
$query->select('id')
->from('tableB')
->where('phone', '=',$phoneNumber);
})->get();
If there is relationship between tableA and tableB you can do following
TableA::where('type',1)
->whereHas('tableBRelationshipName', function ($q) use ($phoneNumber) {
$q->select('id')
$q->where('phone','=',$phoneNumber);
})->get();

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

Laravel Eloquent - Eager Loading Query is Being Restricted by limit()

I have the following query:
MyTable::where('my_column', '=', 25)->with('myOtherTable')
->orderBy('id', DESC)->limit(5);
I would like the above to bring me results analogoous to the following raw SQL query:
SELECT *
FROM myTable AS ABB1
LEFT JOIN myOtherTable AS ABB2 ON ABB1.id = ABB2.myTable_id
WHERE my_column = 25
ORDER BY myTable.id DESC
LIMIT 5;
The above will find everything in myTable along with corresponding info from myOtherTable and then limit the results to 5 rows.
When I run the eloquent statement above, two SQL queries are processed. The first looks something like:
SELECT *
FROM myTable
WHERE my_column = 25
ORDER BY id DESC;
If this query returns say 7 result items, but I pass in a smaller number into the limit() function (ie. limit(5)), then the corresponding eager loading query will look like:
SELECT *
FROM myOtherTable
WHERE id IN(1, 2, 3, 4, 5);
The eager loading query is itself limited to 5 items. There should be no limit here. The number of items in the IN conditional above should be 7 (or whatever the count returned from the first query is). The limit should only be applied after the second query runs.
How would I do this with Eloquent?
You can use eloquent's join.
MyTable::join('myOtherTable', 'column_id', '=', 'id')
->where('my_column', '=', 25)
->with('myOtherTable')
->orderBy('id', DESC)
->limit(5);
The with is only if you want to eager load the relation on MyTable.

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

Using group by and order by in Doctrine

I have one database table called ms_message: with 6 columns (id, senderid, receiverid, content, isRead, receivedTime). I want to get the latest received message group by sender in Doctrine but I cannot run the sub query or use order by and group by as well.
The query in SQL looks like this:
SELECT * FROM (SELECT * FROM ms_message WHERE receiverId = :receiver ORDER BY receivedTime) GROUP BY senderId;
I don't think that Doctrine supports queries like SELECT * FROM (SELECT ...)

Resources