1. Go to this page and download the library: Download andydefer/laravel-utils 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/ */
andydefer / laravel-utils example snippets
use AndyDefer\LaravelUtils\Proxies\TransformableProxy;
use App\ValueObjects\SlugVO;
// Depuis une chaîne
$slug = TransformableProxy::make(SlugVO::class, 'mon-slug');
// Depuis un tableau
$user = TransformableProxy::make(UserRecord::class, [
'name' => 'John Doe',
'email' => '[email protected]',
]);
// Depuis du JSON
$product = TransformableProxy::make(ProductRecord::class, '{"name":"Laptop","price":999}');
// Avec nullable
$coordinates = TransformableProxy::make(CoordinatesVO::class, null, nullable: true);
use AndyDefer\LaravelUtils\Proxies\AttributeProxy;
use App\ValueObjects\SlugVO;
use App\ValueObjects\CoordinatesVO;
use App\Records\SettingsRecord;
class User extends Model
{
// Attribut nullable (slug)
protected function slug(): Attribute
{
return AttributeProxy::nullable(SlugVO::class, column: 'slug');
}
// Attribut ribut avec transformation personnalisée
protected function customSlug(): Attribute
{
return AttributeProxy::nullable(
SlugVO::class,
column: 'slug',
get: function ($value, $attributes) {
return strtolower(trim($value));
},
set: function ($value) {
return ['slug' => strtolower(trim($value))];
}
);
}
}
// Utilisation
$user = User::find(1);
echo $user->slug->getValue(); // 'john-doe'
echo $user->coordinates->getLatitude()->getValue(); // 48.8566
echo $user->settings->theme; // 'dark'
class Product extends Model
{
protected function slug(): Attribute
{
// Le set est automatiquement géré
return AttributeProxy::nullable(SlugVO::class, column: 'slug');
}
}
// La valeur est automatiquement normalisée avant stockage
$product->slug = 'Mon Article'; // Stocké comme 'mon-article'