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,
    ) {
    }
}

$model->data_repository()->add($data);

$song1 = new SongData(
    title: 'Walk', 
    artist: 'Pantera'
);

$model->data_repository()->add($data);

$song2 = new SongData(
    title: 'Cowboys From Hell', 
    artist: 'Pantera'
);

$model->data_repository()->add(
    data: $song2,
    valuesAttributes: [
        'values->title' => $song2->title,
    ]
);

$song3 = new SongData(
    title: 'Davidian', 
    artist: 'Machine Head'
);

$model->data_repository()->add(
    data: $song3,
    valuesAttributes: [
        'values->artist' => $song3->artist,
    ]
);

$model->data_repository();

$model->data_repository(dataClass: SongData::class);

$song = $model->data_repository(dataClass: SongData::class)->values();

$model->data_repository(
    dataClass: SongData::class,
    valuesQuery: ['title' => 'Walk']
);

$table->unsignedBigInteger('dto_attribute')->nullable();

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