Skip to content

Instantly share code, notes, and snippets.

@xmsi
Last active March 18, 2025 05:51
Show Gist options
  • Save xmsi/63fa0e32803cd1a4d4278ec6b618017d to your computer and use it in GitHub Desktop.
Save xmsi/63fa0e32803cd1a4d4278ec6b618017d to your computer and use it in GitHub Desktop.
Helper for Laravel

Sail

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    laravelsail/php82-composer:latest \
    composer install

to install composer with sail on fresh pulled repository\

  1. configure .env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=sail
DB_PASSWORD=password
  1. start sail up
  2. migartions, key generation
  3. sail npm install, then run dev

VS Code Laravelintleisense error SAIL

/var/www/html/ - in both paths

sail php -r "{code}" 
  • PHP command

Sail add service (pgsql) to working docker-compose

> sail artisan sail:install pgsql

will add into docker-compose.yml additional postgresql service, but it can be outdated, so modify to new one manually and don't afraid to change docker-compose

Save to file logs

\Illuminate\Support\Facades\Storage::put('test.txt', print_r($paymentsResponse->isAuthorised(), true));

but this approach saves telescope sql queries also. So in this case it is better to use:

 DB::listen(fn ($query) =>
            file_put_contents(
                storage_path().'/logs/backbone_sync_products_sql.log',
                "\n $query->sql. Bindings: ". implode(',', $query->bindings),
                FILE_APPEND | LOCK_EX)
        );

Laravel Tinker IntellijIdea install

  1. Make sure your CLI interpreter is configured
  2. Plugin's: Project Settings
  3. Make sure to select right interpreter

DB runtime logging mechanism

New Laravel Way. Should be before SQL query

DB::listen(fn ($query) => file_put_contents(storage_path() . '/logs/db.log', "\n{$query->sql}", FILE_APPEND | LOCK_EX));

Old Laravel way.

\DB::enableQueryLog();

// Here lives SQL queries. 

foreach (DB::getQueryLog() as $queryArray) {
     file_put_contents(storage_path() . '/logs/db2.log', "\n".$queryArray['query'], FILE_APPEND | LOCK_EX);
}
        
dd(\DB::getQueryLog());

Foreign Keys

$table->foreignId('type_id')->nullable()->default(null)->constrained('folder_types');

Definitions

  1. public $bindings prop in a ServiceProvider , connect interface (key of an array) with a concreate class (value of an array)
  2. Observers are classes that allow you to listen for certain events that happen to your models, such as creating, updating, deleting, or saving records
  3. Listeners: Usually, a listener is a class that handles a single event. You register the listener for a specific event in the EventServiceProvider.
    Subscribers: A subscriber, on the other hand, is a class that can subscribe to multiple events and define the corresponding event handling methods. It allows you to group related event handling logic together in a single class.
  4. Route->middleware('can:smthing.create') - can is a default Laravel middleware

Laravel versions

https://laravelversions.com/en

DataTable

  1. Transformers are used to format, transform, or modify the data before it is sent to the frontend. Transformers allow you to customize how the data is presented in the DataTable, making it more user-friendly or specific to the needs of your application.
  2. Mappers refer to functions or methods that map data from your model or database to the format or structure required by your DataTable

StateMachine

Why Use a State Machine?
  • Controlled Transitions: A state machine ensures that an entity can only move from one state to another in a predefined way, preventing invalid state changes.
  • Business Logic Enforcement: It helps enforce business rules by allowing transitions only when certain conditions are met.
  • Simplified Management: It simplifies the management of complex workflows, where an entity might need to go through multiple states in a sequence.
Example Scenario: Order Processing

Let's say you have an e-commerce platform, and you need to manage the different states of an order:

  1. States:

    • pending
    • processing
    • shipped
    • delivered
    • canceled
  2. Allowed Transitions:

    • pendingprocessing
    • processingshipped
    • shippeddelivered
    • Any state → canceled (except delivered)

Useful links

@todo: https://laravel-news.com/php-codesniffer-with-laravel
https://laravel-news.com/spatie-image-v3
https://dev.to/etienneleba/test-all-your-business-logic-in-less-than-1-second-2n84
https://dev.to/adnanbabakan/implement-uuid-primary-key-in-laravel-and-its-benefits-55o3 \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment