Skip to content

Instantly share code, notes, and snippets.

@batFormat
Created May 30, 2019 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batFormat/a8df2041686d4e64643612c064a2d826 to your computer and use it in GitHub Desktop.
Save batFormat/a8df2041686d4e64643612c064a2d826 to your computer and use it in GitHub Desktop.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Rinvex\Attributes\Models\Attribute;
class AttributeValuesDelete implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var Model $entity
*/
private $entity;
private $key;
/**
* Create a new job instance.
*
* @param $entity
* @param string $key
*/
public function __construct($entity, $key)
{
$this->entity = $entity;
$this->key = $key;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$attribute = $this->entity->getEntityAttributes()->get($this->key);
if (($relation = $attribute->getAttribute('slug'))
&& ($values = $this->entity->getRelationValue($relation)) && !$values->isEmpty()) {
// Calling the `destroy` method from the given $type model class name
// will finally delete the records from database if any was found.
// We'll just provide an array containing the ids to be deleted.
forward_static_call_array(
[Attribute::getTypeModel($attribute->getAttribute('type')), 'destroy'],
[$values->pluck('id')->toArray()]
);
}
}
}
@batFormat
Copy link
Author

 if (!empty($value)) {
        $model->{$attribute} = $value;
    } else {
        dispatch(new AttributeValuesDelete($model, $attribute));
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment