https://github.com/pfrug/dbhelper
Laravel helper to print statements generated by the Eloquent ORM
https://github.com/pfrug/dbhelper
eloquent helpers laravel php sql
Last synced: about 2 months ago
JSON representation
Laravel helper to print statements generated by the Eloquent ORM
- Host: GitHub
- URL: https://github.com/pfrug/dbhelper
- Owner: pfrug
- Created: 2022-06-03T14:22:42.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-24T17:24:02.000Z (almost 3 years ago)
- Last Synced: 2025-02-02T16:16:54.835Z (3 months ago)
- Topics: eloquent, helpers, laravel, php, sql
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DbHelper
The method **sqlFromBindings** combine Builder functions toSql() and getBindings() to obtain as a result the SQL that is going to be executed.## Usage
``` php
$query = Festivity::where('title', 'like', '%test%')
->where('user_id', Auth::user()->id )
->where('category_id', 5)
->where('country_id', 2)
->where('date_from', '>' , \DB::raw('now()'))
->orderBy('created_at', 'DESC');dbHelper::sqlFromBindings($query);
/*
output:
select *
from `festivity`
where `title` like '%test%' and `user_id` = 1
and `category_id` = 5
and `country_id` = 2
and `date_from` > now()
order by `created_at` desc, `created_at` desc
*/```
:warning: **The query is only for the example. Please don't write queries like this in your controller, Use Scopes**:
The same query but using scopes:
``` php
$query = Festivity::search('test')
->byAuthUser()
->byCategory($categoryIid)
->byCountry($countryId)
->future();
->latest();```
Other example
``` php
$query = \DB::table('users')
->join('contacts', function ($join) {
$join->on(function($query){
$query->on('users.id', '=', 'contacts.user_id')
->orOn("contacts.phone",'users.phone');
});
})
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price');dbHelper::sqlFromBindings($query);
/*
output: (without format)
select `users`.*, `contacts`.`phone`, `orders`.`price`
from `users`
inner join `contacts` on (`users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `users`.`phone`)
inner join `orders` on `users`.`id` = `orders`.`user_id`
*/
```