1. Go to this page and download the library: Download ibekzod/microcrud 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/ */
ibekzod / microcrud example snippets
namespace App\Models;
use Microcrud\Abstracts\Model;
class Product extends Model
{
protected $fillable = [
'name',
'description',
'price',
'stock',
'category_id',
'is_active'
];
// Define relationships as usual
public function category()
{
return $this->belongsTo(Category::class);
}
}
namespace App\Services;
use Microcrud\Abstracts\Service;
use App\Models\Product;
class ProductService extends Service
{
protected $model = Product::class;
// That's it! You now have full CRUD functionality
// Optionally enable advanced features:
// protected $enableCache = true;
// protected $useJob = true;
}
namespace App\Http\Controllers;
use Microcrud\Http\CrudController;
use App\Services\ProductService;
class ProductController extends CrudController
{
protected $service = ProductService::class;
// Optionally override specific methods for custom logic
}
// Example: Add aggregation for grouped queries
class ApartmentService extends Service
{
public function beforeIndex()
{
$data = $this->getData();
$query = $this->getQuery();
if (!empty($data['group_bies'])) {
$table = $this->getModelTableName();
$query->selectRaw("COUNT(*) as apartment_count")
->selectRaw("SUM(price) as total_value")
->selectRaw("AVG(price) as avg_price");
}
$this->setQuery($query);
return parent::beforeIndex();
}
}
class Apartment extends Model
{
public function block()
{
return $this->belongsTo(Block::class);
}
}
class Block extends Model
{
public function manager()
{
return $this->belongsTo(User::class, 'manager_id');
}
}
class ProductService extends Service
{
protected $model = Product::class;
// Override to customize rules
public function createRules($rules = [], $replace = false)
{
return parent::createRules([
'name' => 'tion updateRules($rules = [], $replace = false)
{
return parent::updateRules([
'name' => 'sometimes|string|max:255',
'price' => 'sometimes|numeric|min:0',
'stock' => 'sometimes|integer|min:0',
], $replace);
}
}
class ProductService extends Service
{
protected $model = Product::class;
protected $enableCache = true;
protected $cacheExpiration = 3600; // seconds
// Cache is automatically:
// ✓ Created on read operations
// ✓ Tagged by model name
// ✓ Invalidated on create/update/delete
// ✓ Scoped to query parameters
}
// In your service
$service->delete($id); // Soft delete
$service->delete($id, true); // Force delete
$service->restore($id); // Restore
class ProductService extends Service
{
protected $model = Product::class;
// Before hooks (can modify data)
public function beforeCreate($data)
{
$data['sku'] = 'PRD-' . strtoupper(uniqid());
$data['slug'] = Str::slug($data['name']);
return $data;
}
public function beforeUpdate($id, $data)
{
Log::info("Updating product {$id}", $data);
return $data;
}
// After hooks (receive created/updated item)
public function afterCreate($item)
{
Cache::tags(['products'])->flush();
event(new ProductCreated($item));
return $item;
}
public function afterUpdate($item)
{
event(new ProductUpdated($item));
return $item;
}
public function afterDelete($item)
{
Storage::deleteDirectory("products/{$item->id}");
return $item;
}
public function afterRestore($item)
{
event(new ProductRestored($item));
return $item;
}
public function afterIndex()
{
// Called after listing items
}
}
class ProductService extends Service
{
protected $model = Product::class;
protected $useTransaction = true; // default
// Or disable for specific operations
public function create($data)
{
$this->setIsTransactionEnabled(false);
return parent::create($data);
}
}
class ProductService extends Service
{
protected $model = Product::class;
public function index($data)
{
// Apply custom query before processing
$query = $this->model::query()
->with(['category', 'images'])
->where('is_active', true)
->whereHas('category', function($q) {
$q->where('active', true);
});
$this->setQuery($query);
return parent::index($data);
}
}
class ProductService extends Service
{
protected $model = Product::class;
public function index($data)
{
// Eager load relationships
$query = $this->model::with([
'category',
'images',
'reviews' => function($q) {
$q->where('approved', true);
}
]);
$this->setQuery($query);
return parent::index($data);
}
public function show($id)
{
$query = $this->model::with(['category', 'images', 'reviews']);
$this->setQuery($query);
return parent::show($id);
}
}
$service->withoutScopes(['ActiveScope'])->index($data);
$service->withoutScopes()->index($data); // Remove all scopes
use Microcrud\Traits\ParentChildTrait;
class Category extends Model
{
use ParentChildTrait;
protected $fillable = ['name', 'parent_id'];
}
$category = Category::find(1);
// Get direct children
$children = $category->children;
// Get all descendants (recursive)
$allDescendants = $category->allChildren;
// Get parent
$parent = $category->parent;
// Get all descendant IDs
$ids = $category->getAllDescendantIds(); // [2, 3, 4, 5, ...]
// Get full tree from root
$tree = Category::getRootWithChildren();
$category->delete(); // Automatically deletes all children