akaunting/app/Traits/Media.php

64 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Traits;
use Plank\Mediable\Mediable;
/**
* Mediable Trait.
*
* Provides functionality for attaching media to an eloquent model.
*
* @author Sean Fraser <sean@plankdesign.com>
*
* Whether the model should automatically reload its media relationship after modification.
*/
trait Media
{
use Mediable;
/**
* Relationship for all attached media.
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function media()
{
$media = $this->morphToMany(config('mediable.model'), 'mediable')
->withPivot('tag', 'order')
->orderBy('order');
// Skip deleted media if not detached
if (config('mediable.detach_on_soft_delete') == false) {
$media->whereNull('deleted_at');
}
return $media;
}
2021-05-17 22:32:45 +03:00
public function attachMedia($media, $tags): void
{
$tags = (array)$tags;
$increments = $this->getOrderValueForTags($tags);
$ids = $this->extractPrimaryIds($media);
foreach ($tags as $tag) {
$attach = [];
2021-09-10 11:18:05 +03:00
2021-05-17 22:32:45 +03:00
foreach ($ids as $id) {
$attach[$id] = [
'company_id' => company_id(),
2021-09-10 11:18:05 +03:00
'created_from' => source_name(),
'created_by' => user_id(),
2021-05-17 22:32:45 +03:00
'tag' => $tag,
'order' => ++$increments[$tag],
];
}
2021-09-10 11:18:05 +03:00
2021-05-17 22:32:45 +03:00
$this->media()->attach($attach);
}
$this->markMediaDirty($tags);
}
}