PHP code example of evanschleret / laravel-typebridge

1. Go to this page and download the library: Download evanschleret/laravel-typebridge 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/ */

    

evanschleret / laravel-typebridge example snippets




declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\User;
use EvanSchleret\LaravelTypeBridge\Attributes\TypeBridgeResource;
use Illuminate\Http\Resources\Json\JsonResource;

#[TypeBridgeResource(
    name: 'UserItem',
    structure: [
        'id' => 'number',
        'email' => 'string|null',
        'roles' => '@relation(roles)',
        'manager?' => '@relation(manager)',
    ],
)]
final class UserResource extends JsonResource
{
    public static string $model = User::class;
}



declare(strict_types=1);

return [
    'output' => [
        'base_path' => resource_path('typescript'),
        'additional_paths' => [],
    ],
    'sources' => [
        app_path('Http/Resources'),
    ],
    'generation' => [
        'use_semicolons' => false,
        'indent_size' => 2,
        'generate_index' => true,
        'shared_file' => '_api',
        'shared_append' => [],
        'append_templates' => [],
    ],
    'files' => [
        'extension' => 'ts',
        'naming_pattern' => '{name}',
    ],
];

'generation' => [
    'indent_size' => 4,
    'shared_file' => '_api',
    'shared_append' => [
        'export interface ApiItemResponse<T> {',
        "  status: 'success' | 'error'",
        '  data: T',
        '}',
        'export interface ApiCollectionResponse<T> {',
        '  data: T[]',
        '}',
    ],
    'append_templates' => [
        [
            'name_ends_with' => 'Item',
            'lines' => [
                'export type {base} = ApiItemResponse<{name}>',
                'export type {basePlural} = ApiCollectionResponse<{name}>',
            ],
        ],
    ],
],

'files' => [
    'naming_pattern' => '{kebab}.types',
],
bash
php artisan vendor:publish --provider="EvanSchleret\\LaravelTypeBridge\\TypeBridgeServiceProvider" --tag=typebridge-config
bash
php artisan typebridge:generate
bash
php artisan typebridge:generate --output-path=resources/typescript
bash
php artisan typebridge:generate --dry-run
bash
php artisan typebridge:generate --clean
bash
php artisan typebridge:generate --only=User,Role
bash
php artisan typebridge:generate --except=Membership
bash
php artisan typebridge:check