PHP code example of raqmeyat / smart-orm

1. Go to this page and download the library: Download raqmeyat/smart-orm 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/ */

    

raqmeyat / smart-orm example snippets


use Illuminate\Database\Eloquent\Model;
use Raqmeyat\SmartOrm\Concerns\HasSmartFetch;

class Post extends Model
{
    use HasSmartFetch;

    protected $fillable = ['author_id', 'title', 'body', 'views', 'status'];
}

Post::query()->smartSelect()->paginate();               // reads ?fields=title,views
Post::query()->smartSelect('title,views')->get();       // explicit
Post::query()->smartSelect($request, ['author_id'])     // force columns your eager loads need
    ->with('author')->get();

public function smartFetchable(): array
{
    return ['title', 'views', 'created_at', 'id'];
}

final class PostFilter extends Filter
{
    public function filters(): array
    {
        return [
            'id'           => FilterOperator::numeric(),
            'title'        => FilterOperator::text(),
            'views'        => FilterOperator::numeric(),
            'status'       => FilterOperator::choice(),
            'published_at' => FilterOperator::temporal(),
            'author.name'  => FilterOperator::text(),   // relation filter (whereHas)
        ];
    }

    public function sorts(): array         { return ['id', 'views', 'published_at']; }
    public function defaultSort(): ?string { return '-published_at'; }
}

Post::query()->smartFilter(PostFilter::class)->smartSelect()->paginate();
Post::query()->smartFilter()->paginate();   // App\Filters\PostFilter, found by convention

public function custom(): array
{
    return [
        'search' => fn (Builder $query, mixed $term) => $query->where(
            fn (Builder $q) => $q->where('title', 'like', "%{$term}%")->orWhere('body', 'like', "%{$term}%"),
        ),
    ];
}

final readonly class PostData extends Data
{
    public function __construct(
        public int $id,
        public int $author_id,
        public string $title,
        public ?string $body,
        public int $views,
        public PostStatus $status,          // enum cast wins over the raw string column
        public bool $is_published,
        public ?CarbonImmutable $published_at,
        public ?array $meta,
    ) {}
}

PostData::fromModel($post);                  // full typed object (all attributes must be loaded)
PostData::collect($posts);                   // list<PostData>
$data->toArray();                            // scalars only: ISO-8601 UTC dates, enum values
PostData::partial($post);                    // ONLY the attributes a smartSelect() loaded
PostData::collectPartial($posts);

return PostData::collectPartial(Post::query()->smartFilter()->smartSelect()->paginate()->items());
bash
php artisan vendor:publish --tag=smart-orm-config   # config/smart-orm.php
php artisan vendor:publish --tag=smart-orm-stubs    # stubs/smart-orm/*.stub (customise generated code)
bash
php artisan make:data-model Post