Alicia handler trait
Alicia handler trait comes with relation definition and some helper methods to help you to keep your code simple.
Available methods
attachments
The relationship definition to Hans\Alicia\Models\Resource
model.
use Hans\Alicia\Facades\Alicia;
$model = Alicia::upload( request()->file('file') )->getData();
$post->attachments()->sync( $model );
attachment
Returns the oldest attached file.
use Hans\Alicia\Facades\Alicia;
$modelB = Alicia::external( 'https://www.file-server.dev/files/file-name.extension' )->getData();
$modelA = Alicia::upload( request()->file('file') )->getData();
$post->attachTo( $modelA );
$post->attachTo( $modelB );
$post->attachment(); // returns $modelA
deleteAttachments
Detaches and deletes all related resources.
use Hans\Alicia\Facades\Alicia;
$modelB = Alicia::external( 'https://www.file-server.dev/files/file-name.extension' )->getData();
$modelA = Alicia::upload( request()->file('file') )->getData();
$post->attachTo( $modelA );
$post->attachTo( $modelB );
$post->deleteAttachments();
attachTo
Using this method, you can attach a resource to your model instance.
use Hans\Alicia\Facades\Alicia;
$model = Alicia::upload( request()->file('file') )->getData();
$post->attachTo( $model );
By the way, you can set a key for your attachments.
use Hans\Alicia\Facades\Alicia;
$model = Alicia::upload( request()->file('file') )->getData();
$post->attachTo( $model,'avatar' );
attachManyTo
It’s like attachTo
method but, you can attach multiple resources at once.
use Hans\Alicia\Facades\Alicia;
$modelB = Alicia::external( 'https://www.file-server.dev/files/file-name.extension' )->getData();
$modelA = Alicia::upload( request()->file('file') )->getData();
$post->attachManyTo( [
$modelA->id,
$modelB->id => [ 'key' => $key = 'avatar' ],
] );