cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
537 stars 72 forks source link

makeTagArray fails on null in TagService.php returns error #109

Closed kinsaz closed 4 years ago

kinsaz commented 4 years ago

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!!!

cviebrock commented 4 years ago

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