Just use normal sql with getPdo ().
Converting MS Access Queries to Laravel Query Builder
I am converting some queries that contain primarily aggregate fields, all in one query, in a MS Access db.
How would I convert so many aggregate fields into one lookup? Or would I need to break it up and then combine them?
For instance, some of the query fields look like this:
Field: Product Name Table: Products Total: Group By
Field: Product Name Table: Products Total: Count Criteria: >2
Field: Price Table: Products Total: Avg Sort: Descending
Field: Price Table: Products Total: StDev
Which lists all products with their counts of sales and their avg and standard deviation of prices.
How would I do this in Eloquent?
Sometimes, you can spend a great deal of time trying to make a query work "the eloquent way". Don't waste time. First, eloquent can't do everything. It's an ORM and not a drop in replacement for everything. There are many times you have to do raw queries since eloquent doesn't even have all of the SQL language implemented. Like if you want to do CASE statements, or REPLACE statements, you have to do it raw. Eloquent doesn't implement those. Eloquent imlements probably the most widely used sql functions, but it's probably only about 5% of what's available. ORMs are mainly for rapid prototyping. ORMs will always be slower than raw sql (and take more memory/resources), as php has to first build up the query that the ORM generates in order to send it to the db. Knowing how to actually use sql is far more important in coding than knowing how to use an ORM.
The main thing you have to do is make sure the query is safe from sql injections, mainly by binding parameters (ESPECIALLY IF SOMETHING IS COMING FROM USER INPUT). You can always do things like:
$users = DB::select('select * from users where active = ?', [1]);
or use placeholders instead of ?'s
$results = DB::select('select * from users where id = :id', ['id' => 1]);
Examples above taken from the user guide. Here's more: https://laravel.com/docs/5.6/queries#raw-expressions
Most of my complex reports use pure sql.
Please or to participate in this conversation.