PHP code example of abetwothree / laravel-ts-publish
1. Go to this page and download the library: Download abetwothree/laravel-ts-publish 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/ */
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
#[TsEnumMethod]
public function label(): string
{
return match($this) {
self::Active => 'Active User',
self::Inactive => 'Inactive User',
};
}
}
class User extends Model
{
public function casts(): array
{
return ['status' => Status::class];
}
protected function initials(): Attribute
{
return Attribute::get(fn (): string => /* ... */);
}
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
}
/** @mixin User */
class UserResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'role' => EnumResource::make($this->role),
'posts' => PostResource::collection($this->whenLoaded('posts')),
];
}
}
class StorePostRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => [';
}
}
class OrderShipped implements ShouldBroadcast
{
public function __construct(
public int $orderId,
public string $trackingNumber,
public string $carrier,
) {}
public function broadcastOn(): Channel
{
return new PrivateChannel("orders.{$this->orderId}");
}
}
class HandleInertiaRequests extends Middleware
{
public function share(Request $request): array
{
return [
...parent::share($request),
'auth' => ['user' => $request->user()],
];
}
}
use AbeTwoThree\LaravelTsPublish\Attributes\TsExtends;
#[TsExtends('HasTimestamps', import: '@/types/common')]
#[TsExtends('Pick<Auditable, "created_by" | "updated_by">', import: '@/types/audit', types: ['Auditable'])]
class Warehouse extends Model
{
// ...
}
use AbeTwoThree\LaravelTsPublish\Attributes\TsExclude;
class User extends Model
{
#[TsExclude]
protected function secretToken(): Attribute
{
return Attribute::make(get: fn (): string => 'hidden');
}
}