PHP Laravel cviebrock/eloquent-sluggable not working because "instanceof SluggableInterface" always returns false

StackOverflow https://stackoverflow.com/questions/23421140

Question

I'm trying to use the Laravel's sluggable package found here: https://github.com/cviebrock/eloquent-sluggable

When I save a model (ex. Product), the sluggable feature doesn't sluggify my model. This is on a brand new install of Laravel as of today.

I can see that the event listener fires (line 43 in SluggableServiceProvider.php), but it seems as though the if ($model instanceof SluggableInterface) statement never returns true and never sluggifies my model. Could this be a name spacing issue? Any other ideas?

Here is my Product model:

<?php
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;

class Product extends Eloquent {

  use SluggableTrait;
  protected $sluggable = array(
      'build_from' => 'title',
      'save_to'    => 'slug',
  );
}

I can, of course, get the sluggify method to run manually by doing something like this:

$product->sluggify();

But I would like to adhere to best practices and try to get the sluggify code to work automatically when the model save event is triggered.

Était-ce utile?

La solution

add implements SluggableInterface to your Product class

class Product extends Eloquent implements SluggableInterface 
{

}

demo code on github:

https://github.com/cviebrock/eloquent-sluggable#updating-your-eloquent-models

Autres conseils

you should implement SluggableInterface

To resolve the issue of incompatible declarations or syntaxes with the latest PHP versions, I downgraded my PHP Version to 7.4.

PHP version to 7.4

This fixed the problem I was experiencing. It is important to note that using an older version of PHP, such as version 7.4, may resolve compatibility issues that may arise when using newer versions of PHP. However, it is always a good idea to keep your PHP version up to date to take advantage of the latest features and security updates.

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top