PHP code example of diephp / laravel-resources-typescript

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

    

diephp / laravel-resources-typescript example snippets


'resources_dir' => 'app/Http/Resources',
'output_typescript_file' => 'resources/ts/Resources.ts',

class ExampleResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => (int) $this->id,
            'name' => (string) $this->name,
        ];
    }
}

use JetBrains\PhpStorm\ArrayShape;

class ExampleResource extends JsonResource
{
    #[ArrayShape(['id' => 'int', 'name' => 'string'])]
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}

class ExampleResource extends JsonResource
{
    /**
     * @return array{
     *     id: int,
     *     name: string,
     *     category: \App\Http\Resources\CategoryResource,
     * }
     */
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'category' => new CategoryResource($this->category),
        ];
    }
}

class UserDto
{
    public string $name;
    public string $email;
    public int $age;
}

/**
 * @property string $domain
 * @property mixed $protocol
 */
class SiteModel extends Model
{
}

class ContactModel extends Model
{
    protected $fillable = ['name', 'email', 'phone'];
}

class TimeResource extends JsonResource
{
    public function toArray($request): array
    {
        $result = [];
        $result['time'] = $this->resource->time;

        if ($this->resource->time_zone) {
            $result['time_zone'] = $this->resource->time_zone;
        }

        return $result;
    }
}

enum StatusEnum: string
{
    case Draft = 'draft';
    case Published = 'published';
}

class StatusResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'status' => StatusEnum::Published,
        ];
    }
}

class EnumValueResource extends JsonResource
{
    public function toArray($request)
    {
        return StatusEnum::Published;
    }
}
bash
php artisan vendor:publish --tag=resources2typescript
bash
php artisan diephp:generate-typescript-interfaces