I am familiar with the love of Eloquent among the laravel community but sometimes, sometimes its better to go for raw queries. Its saves you a lot of time and trouble.
Trust me, its hard to digest but is true.
Lets consider a case where the following query is a complex one.
select count(id) from users where admin_id = 2
Create a queries directory in storage. storage/queries
, you can create further sub directories as per requirement.
Add the following method to you helper methods.
if (!function_exists('getQuery')) {
/**
* @param $queryName
* @param null $data
* @return array|false|string|string[]
*/
function getQuery($queryName, $data = null)
{
$query = file_get_contents(storage_path('queries/' . $queryName . '.sql'));
if ($data) {
foreach ($data as $key => $value) {
$query = str_replace($key, $value, $query);
}
}
return $query;
}
}
once the directory and helper function is in place all you have to do is create SQL query files in the storage/queries
directory
say, storage/queries/admin.users.sql
containing the query,
select count(id) from users where admin_id = adminId
Please make note, the dynamic ids are replaced with a variable name.
Call the query with helper function as follows.
use Illuminate\Support\Facades\DB;
/**
* @return array
*/
public function adminUsers(): array
{
return DB::select(getQuery('admin.users', [
'adminId' => auth()->id()
]));
}
In this way, you can get any complex query data, without going through the trouble of Eloquent.
Feel free to provide us with your review and feedback in the form of comments.
AppImages are the most easiest to run compared.The challenging task is to make the AppImage accessible globally through he system like an installed application (in the menu)
#code-quality #vscode #laravel-pint #laravel
I have been using the unsatisfactory formatters from a long time. All of them have one thing but lack other. Laravel Pint is one who has it all, combining it with vscode is like proving you laravel project a super power.
#express js #node #node js #sequelize #mysql
In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.
#database #javascript #sequelize #npx
list of Sequelize CLI commands which comes handy, while working on a project.
#functionality #pwa #javascript #laravel
The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA
#code-quality #vscode #laravel-pint #laravel
I have been using the unsatisfactory formatters from a long time. All of them have one thing but lack other. Laravel Pint is one who has it all, combining it with vscode is like proving you laravel project a super power.
#functionality #pwa #javascript #laravel
The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA
As you may have known by now that laravel 6.0 has been out. Though it's expected to have a lot of changes in the major release but in 6.0 there are not that many changes.
below are mentioned things to be taken care of at the time of deploying a laravel project to production.
When you make a refresh in a react app in any page apart from root, if you are facing 404 add the below line of code in your nginx configuration file.