PHP code example of vicgutt / laravel-auto-model-cast
1. Go to this page and download the library: Download vicgutt/laravel-auto-model-cast 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/ */
vicgutt / laravel-auto-model-cast example snippets
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use VicGutt\AutoModelCast\Contracts\AutoCastable;
use VicGutt\AutoModelCast\Concerns\HasAutoCasting;
final class MyModel extends Model implements AutoCastable
{
use HasAutoCasting;
}
// a migration file
Schema::create('examples', function (Blueprint $table): void {
$table->json('extras');
});
// a model file
use Illuminate\Database\Eloquent\Casts\AsCollection;
final class Example extends Model implements AutoCastable
{
use HasAutoCasting;
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'extras' => AsCollection::class,
];
}
use VicGutt\AutoModelCast\Support\TypeMapper;
final class MyCustomTypeMapper extends TypeMapper
{
//
}
Casts::new()->useTypeMapper(MyCustomTypeMapper::class);
use VicGutt\InspectDb\Entities\Column;
use Illuminate\Database\Eloquent\Model;
use VicGutt\AutoModelCast\Support\Casters\BaseCaster;
final class MyCustomCaster extends BaseCaster
{
public function handle(Column $column, Model $model): ?string
{
//
}
}