1. Go to this page and download the library: Download devio/laravel-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/ */
devio / laravel-model-cast example snippets
class Car extends Model
{
use CastsModel;
// ...
}
class TeslaCar extends Car
{
}
$instance = Car::find(1);
$instance::class; // -> TeslaCar
class Car extends Model
{
use CastsModel;
public function getCastedModelClass(array $attributes): string|null
{
// Add your custom resolution logic here based on the model attributes
if ($attributes['brand'] == 'tesla') return TeslaCar::class;
if ($attributes['brand'] == 'ford') return FordCar::class;
return null;
}
}
class TeslaCar extends Car {}
class FordCar extends Car {}