PHP code example of a1dbox / laravel-model-accessor-builder

1. Go to this page and download the library: Download a1dbox/laravel-model-accessor-builder 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/ */

    

a1dbox / laravel-model-accessor-builder example snippets


use A1DBox\Laravel\ModelAccessorBuilder\Concerns\HasAccessorBuilder;

class User extends Model
{
    use HasAccessorBuilder;
}

class User extends Model
{
    use HasAccessorBuilder;
    
    public function getFullNameAttribute()
    {
        return AccessorBuilder::make(
            fn (AccessorBuilder\BlueprintCabinet $cabinet) => $cabinet->trim(
                $cabinet->concat(
                    $cabinet->col('name'),
                    $cabinet->str(' '),
                    $cabinet->col('last_name'),
                )
            ),
        );
    }
}

return trim($this->name . ' ' . $this->last_name);

$user = User::query()
    ->withAccessor('full_name')
    ->find(1);

echo $user->full_name; //John Doe

$user = User::find(1);

echo $user->full_name; //John Doe

$users = User::query()
    ->withAccessor('full_name')
    ->orderBy('full_name')
    ->get();

$users = User::query()
    ->withAccessor('full_name')
    ->where('full_name', 'John Doe')
    ->get();

$users = User::query()
    ->withAccessor(['full_name', 'full_address'])
    ->get();