PHP code example of thesoftpulze / laravibe-standards

1. Go to this page and download the library: Download thesoftpulze/laravibe-standards 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/ */

    

thesoftpulze / laravibe-standards example snippets


final readonly class UserProfileDTO implements Arrayable, Jsonable
{
    use \TheSoftPulze\LaravibeStandards\DTOs\Concerns\AsDTO;

    public function __construct(
        public string $name,
        public ?string $bio = null,
    ) {
    }
}

$dto = UserProfileDTO::from($request);
$dto = UserProfileDTO::fromArray(['name' => 'Alice', 'bio' => 'Developer']);

$array = $dto->toArray();
$json  = $dto->toJson();
$data  = $dto->toEloquent();

$updated = $dto->with(['bio' => 'Senior Dev']);

enum ToastType: int
{
    use \TheSoftPulze\LaravibeStandards\Enums\Concerns\HasEnumMetadata;

    case Error = 1;
    case Success = 2;
    case Warning = 3;
    case Info = 4;
}

ToastType::options();    // [{name: 'Error', value: 1, label: 'Error'}, ...]
ToastType::values();     // [1, 2, 3, 4]
ToastType::isValidValue(2);  // true
ToastType::fromValueOrFail(1); // ToastType::Error
ToastType::Error->label();     // 'Error'

final class UserResource extends \TheSoftPulze\LaravibeStandards\Resources\AppResource
{
    public function toArray(Request $request): array
    {
        return [
            $this->id(),
            $this->attribute('name'),
            $this->attribute('email'),
            ...$this->timestamps(),
        ];
    }
}

UserResource::make($user);                           // single
UserResource::collection(User::paginate());          // paginated

// Inertia
return inertia('users/Show', ['user' => UserResource::make($user)->toInertia()]);
bash
php artisan vendor:publish --tag="laravibe-standards"
bash
php artisan vendor:publish --tag="laravibe-standards-config"
bash
php artisan vendor:publish --tag="laravibe-standards-stubs"
bash
php artisan make:dto UserProfileDTO
php artisan make:dto Account/UpdateProfileDTO
bash
php artisan make:resource UserResource
php artisan make:resource UserCollection --collection