cviebrock/eloquent-taggable

makeTagArray fails on null in TagService.php returns error

kinsaz opened this issue · 1 comments

steps taken to reproduce your issue- [ pass null using $model->hasTag('foo')]

Laravel version 7.12.0
PHP version 7.3.2

error = Call to a member function pluck() on null
Cviebrock\EloquentTaggable\Services\TagService::makeTagArray
vendor/cviebrock/eloquent-taggable/src/Services/TagService.php:166

Suggested change to method:

----current----

public function makeTagArray(Model $model, string $field = 'name'): array
{
/** @var Collection $tags */
$tags = $model->tags;

    return $tags->pluck($field)->all();
}

----suggested----

public function makeTagArray(Model $model, string $field = 'name'): array
{
/** @var Collection $tags */
$tags = $model->tags;

  if($tags){
    return $tags->pluck($field)->all();
   }

  return [];

}

Thanks!!!

Sorry for the delay. Please submit a PR with this change (and a test) and I will add it to the package.