Created
May 30, 2019 20:10
-
-
Save batFormat/a8df2041686d4e64643612c064a2d826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()] | |
); | |
} | |
} | |
} |
Author
batFormat
commented
May 30, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment