Hello everyone,
If you need to use a global database connection to use grocerycrud in different controllers without needing to create the _getDatabaseConnection and _getGroceryCrudEnterprise functions in all controllers, this solution could help you.
Steps
1. Create a custom helper in the path app/Helper/name-helper.php
2. name-helper.php code:
if ( !function_exists('_getGroceryCrudEnterprise') )
{
function _getGroceryCrudEnterprise() {
$databaseConnection = config('database.default');
$databaseConfig = config('database.connections.' . $databaseConnection);
//var_dump($databaseConfig);
$database = [
'adapter' => [
'driver' => 'Pdo_Mysql',
'host' => $databaseConfig['host'],
'database' => $databaseConfig['database'],
'username' => $databaseConfig['username'],
'password' => $databaseConfig['password'],
//'unix_socket' => $databaseConfig['unix_socket'],
'charset' => 'utf8'
]
];
$config = config('grocerycrud');
$crud = new GroceryCrud($config, $database);
return $crud;
}
}
if ( !function_exists('_show_output') )
{
function _show_output($output) {
if ($output->isJSONResponse) {
return response($output->output, 200)
->header('Content-Type', 'application/json')
->header('charset', 'utf-8');
}
$css_files = $output->css_files;
$js_files = $output->js_files;
$output = $output->output;
return view('default_template', [
'output' => $output,
'css_files' => $css_files,
'js_files' => $js_files
]);
}
}
Importan 1: Remember that to configure laravel you need to edit the .dev file that is located in the root directory, then you do not need to edit $databaseConfig['host'] or $databaseConfig['database']... etc
Important 2: Is the same concept for the function _show_output, We need to use this function in others controllers.
3. Open up your applications app/Providers/AppServiceProvider.php which looks like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
4. We are going to create a new function inside of the AppServiceProvider called loadHelpers() which wil load all of our PHP helper files.
protected function loadHelpers()
{
foreach (glob(__DIR__.'/../Helpers/*.php') as $filename) {
require_once $filename;
}
}
Importan: Any PHP file that you add to the app/Helpers/ folder will automatically be loaded. So, you can feel free to organize your helper files however you'd like. FYI, you will not have a Helpers folder by default, you will need to create it.
5. Then we'll call the loadHelpers() function inside of our register() method. So our final app/Providers/AppServiceProvider.php file will look like the following:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->loadHelpers();
}
protected function loadHelpers()
{
foreach (glob(__DIR__.'/../Helpers/*.php') as $filename) {
require_once $filename;
}
}
}
6. That's it, now you can call the functions _getGroceryCrudEnterprise and _show_output in all your controllers.
public function customers(Request $request)
{
$crud = _getGroceryCrudEnterprise();
$crud->setTable('customers');
$crud->columns(['customerName','phone','addressLine1','creditLimit']);
$crud->fields(['customerName','phone','addressLine1','creditLimit']);
$output = $crud->render();
return _show_output($output);
}
B)
Regards
Josh
Especial thanks https://devdojo.com/tutorials/custom-global-helpers-in-laravel
