PHP code example of ngocnm / laravel_helpers

1. Go to this page and download the library: Download ngocnm/laravel_helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ngocnm / laravel_helpers example snippets


\Ngocnm\LaravelHelpers\providers\HelperServiceProvider::class

protected $middlewareGroups = [
    ...,
    'api' => [
        ...,
        FilterRequestForApi::class
    ],
];
 

return [
    'paginate' => [
        'page_max' => 30,
        'limit_max' => 100
    ],
    'backup' => [
        'enable' => env('HELPER_BACKUP_ENABLE', true),
//        'ask' => false,
        'backup_driver' => env('HELPER_BACKUP_DRIVER', 'local'),
        'mysqldump_path' => env('HELPER_MYSQLDUMP_PATH', '/usr/bin/mysqldump'),
        'zip_path' => env('HELPER_ZIP_PATH', '/usr/bin/zip'),
        'number_of_backup' => env('HELPER_NUMBER_OF_BACKUP', 5),
        'database' => [
            'folder' => env('HELPER_BACKUP_FOLDER', '/var/www/html/backup/database'),
            'description' => 'Backup database',
            'file_name' => 'database_backup.sql',
            'file_config' => env('HELPER_MYSQL_CONFIG', '/home/huyct/mysql.conf'),
            'database_name' => env('HELPER_DATABASE_NAME', 'laravel'),
            'tables' => [
                'table1',
                'table2'
            ],
        ],
    ],
    'deploy' => [
        'commands' => [
            [
                'enable' => true,
                'description' => 'Update code',
                'folder' => '/var/www/html/code/', //folder run command
                'cmd' => [
                    'git pull origin master',
                    'composer update --no-dev'
                ],
                'user' => '',//User run command (default user ssh)
                'ask' => false
            ]
        ],
        'servers' => [
            [
                'enable' => true,
                'name' => 'ai_master',
                'ip' => '127.0.0.1',
                'user_name' => 'ubuntu'
            ]
        ],
    ],
    'log' => [
        'driver' => env("HELPER_LOG_DRIVER", 'slack'),
        'enable' => env("HELPER_LOG_ENABLE", true),
        'connections' => [
            'slack' => [
                'name' => 'Send Message To Slack',
                'slack_error_url' => env("SLACK_ERROR_URL"),
                'slack_log_url'=>env("SLACK_LOG_URL"),
            ],
        ]
    ]
];


    'ip_server' => env('IP_SERVER', 'localhost'),
    'app_name' => env('APP_NAME0', 'localhost'),
 
namespace App\Models;

class User {
    static $schema = [
        "id" => [
            "type" => "int",
            "insert" => false,
            "query_condition" => true,
            "sort" => true
        ],
        "title" => [
            "type" => "string",
            "insert" => false,
            "query_condition" => true,
            "sort" => true,
            "fulltext_search" => true
        ],
        "created_at" => [
            "type" => "string",
            "insert" => false,
            "query_condition" => false,
            "

    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            \Ngocnm\LaravelHelpers\exceptions\SendLog::SendLog($e);
        }
    }

Ngocnm\LaravelHelpers\Helper::BaseApiRequest()->getFields();
Ngocnm\LaravelHelpers\Helper::BaseApiRequest()->getFields();
...



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Ngocnm\LaravelHelpers\eloquent\BaseModel; // add line

class UserLog extends Model
{
    use HasFactory,BaseModel;// add line
    protected $table = 'user_logs';
}
 

// Filter request 
function getUserBy Api(){
    $model = UserLog::baseQueryBuilder(UserLog::class);//add line
    ...
    return $model->get();
}



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Ngocnm\LaravelHelpers\eloquent\BaseModel;

class UserLog extends Model
{
    use HasFactory,BaseModel;
    protected $table = 'user_logs';
    
    const relationship_device_fields = ['id','name','ip']; // add line
    
    public function device(){ // add function
        return $this->belongsTo(Device::class,  'device_id','id');
    }
}

Ngocnm\LaravelHelpers\Helper::BaseApiRequest()->setLimitMax(100);
Ngocnm\LaravelHelpers\Helper::BaseApiRequest()->setPageMax(100);

protected $middlewareGroups = [
    ...,
    'api' => [
        ...,
        LogQueryForApi::class
    ],
];
bash 
php artisan vendor:publish --tag=helper_config
# Vị trí file config  config/helper.php
dotenv
SLACK_ERROR_URL=
SLACK_LOG_URL=
HELPER_LOG_DRIVER=
HELPER_LOG_ENABLE=
HELPER_LOG_QUEUE_NAME=
IP_SERVER=
APP_NAME=
bash 
php artisan helper:deploy-app
bash
php artisan helper:backup-database