PHP code example of ibrostudio / laravel-data-repository
1. Go to this page and download the library: Download ibrostudio/laravel-data-repository 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/ */
ibrostudio / laravel-data-repository example snippets
namespace App\Models;
use IBroStudio\DataRepository\Concerns\HasDataRepository;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use HasDataRepository;
}
namespace App\DataObjects;
use Spatie\LaravelData\Data;
class SongData extends Data
{
public function __construct(
public string $title,
public string $artist,
) {
}
}
$data = new SongData(
title: 'Walk',
artist: 'Pantera'
);
$data = new \MichaelRubel\ValueObjects\Collection\Complex\Name('Pantera');
$model->data_repository()->add($data);
namespace App\DataObjects;
use Spatie\LaravelData\Data;
use MichaelRubel\ValueObjects\Collection\Complex\Name;
class SongData extends Data
{
public function __construct(
public Name $title,
public Name $artist,
) {
}
}
use IBroStudio\DataRepository\Casts\DataObjectCast;
class YourEloquentModel extends Model
{
protected function casts(): array
{
return [
'song' => DataObjectCast::class,
];
}
}
use IBroStudio\DataRepository\Casts\DataObjectCast;
class YourEloquentModel extends Model
{
protected function casts(): array
{
return [
'song' => DataObjectCast::class,
];
}
}
$model = YourEloquentModel::create([
'song' => new SongData(
title: 'Walk',
artist: 'Pantera'
),
]);
// or
$model->song = new SongData(
title: 'Walk',
artist: 'Pantera'
);
$model->save();
bash
php artisan data-repository:install
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.