1. Go to this page and download the library: Download karim-ashraf/lara-architect 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/ */
karim-ashraf / lara-architect example snippets
use KarimAshraf\LaraArchitect\Architecture\ArchitectureEngine;
$result = ArchitectureEngine::create()->analyze('/path/to/project', ['app']);
use KarimAshraf\LaraArchitect\Database\ArchitectRepository;
/**
* @extends ArchitectRepository<Product>
*/
class ProductRepository extends ArchitectRepository
{
protected function model(): string
{
return Product::class;
}
public function findBySlug(string $slug): ?Product
{
return $this->findBy('slug', $slug);
}
}
$repository->delete($product); // soft delete (or hard delete if the model doesn't soft delete)
$repository->deleteMany([1, 2, 3]); // bulk delete, returns the affected count
$repository->deleteAll(); // delete everything
$repository->restore($product); // bring one back
$repository->restoreAll([1, 2]); // restore given ids — or every trashed record with no arguments
$repository->forceDelete($product); // permanently remove, even when already trashed
$repository->trashed(); // list soft-deleted records
use KarimAshraf\LaraArchitect\Services\ArchitectService;
/**
* @extends ArchitectService<Product>
*/
class ProductService extends ArchitectService
{
public function __construct(ProductRepository $repository)
{
parent::__construct($repository);
}
protected function prepareForCreate(array $data): array
{
$data['slug'] ??= Str::slug($data['name']);
return $data;
}
protected function created(Model $model, array $data): void
{
// dispatch events, clear caches, ...
}
}
use KarimAshraf\LaraArchitect\Actions\ArchitectAction;
class PublishPost extends ArchitectAction
{
protected function handle(Post $post): Post
{
$post->update(['published' => true]);
return $post->refresh();
}
}
// Resolved from the container, executed in a transaction:
PublishPost::run($post);
use KarimAshraf\LaraArchitect\Http\Filters\ArchitectQueryFilter;
class ProductFilter extends ArchitectQueryFilter
{
public function search(string $value): void
{
$this->builder->where(fn ($q) => $q
->where('name', 'like', "%{$value}%")
->orWhere('description', 'like', "%{$value}%"));
}
public function priceMin(string $value): void
{
$this->builder->where('price', '>=', (float) $value);
}
}
Product::filter($filter)->paginate(); // via the Filterable model trait
$repository->filter($filter, perPage: 20); // via the repository
$service->filter($filter); // via the service
use KarimAshraf\LaraArchitect\Support\ArchitectData;
final class ProductData extends ArchitectData
{
public function __construct(
public readonly string $name,
public readonly float $price,
public readonly ?string $description = null,
) {}
}
$data = ProductData::fromRequest($request); // uses validated() on form requests
$data = ProductData::fromArray(['name' => 'Desk', 'price' => 99.9]);
$data->toArray(); // snake_case keys
$data->toFilteredArray(); // nulls removed — great for partial updates
class StoreProductRequest extends ArchitectFormRequest
{
public function rules(): array
{
return ['name' => ['
bash
composer architect:new # generate a module the right way
php artisan architect:lint # enforce layer rules
php artisan architect:analyze # see health, hotspots, structure
php artisan architect:workspace # context + issues + explain (Workspace snapshot)
php artisan architect:ask "why ProductService exists" # Phase 13 — living knowledge query