The : SlugOptions part of the method definition is just a typehint for the return type.
It means that the method must return a SlugOptions object.
See: https://www.brainbell.com/php/type-declarations-hints.html#return-type
Hi, I am using spatie slugable on laravel model , It works like a charm... but I really dont understand how it works can some one explain to me how this calls the options , functions , variables ... when I enter to the definition I found the class slugOptions...
Can explain to me how this works??
whats means this = getSlugOptions() : SlugOptions I mean this sign : : : : the doble dots how this works
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom([ 'name' , 'sponsor_id' ])
->saveSlugsTo('slug');
}
thanks a lot this is the class of reference
<?php
namespace Spatie\Sluggable;
class SlugOptions
{
/** @var array|callable */
public $generateSlugFrom;
public string $slugField;
public bool $generateUniqueSlugs = true;
public int $maximumLength = 250;
public bool $generateSlugsOnCreate = true;
public bool $generateSlugsOnUpdate = true;
public string $slugSeparator = '-';
public string $slugLanguage = 'en';
public array $translatableLocales = [];
public static function create(): self
{
return new static();
}
public static function createWithLocales(array $locales): self
{
$slugOptions = static::create();
$slugOptions->translatableLocales = $locales;
return $slugOptions;
}
/**
* @param string|array|callable $fieldName
*
* @return \Spatie\Sluggable\SlugOptions
*/
public function generateSlugsFrom($fieldName): self
{
if (is_string($fieldName)) {
$fieldName = [$fieldName];
}
$this->generateSlugFrom = $fieldName;
return $this;
}
public function saveSlugsTo(string $fieldName): self
{
$this->slugField = $fieldName;
return $this;
}
public function allowDuplicateSlugs(): self
{
$this->generateUniqueSlugs = false;
return $this;
}
public function slugsShouldBeNoLongerThan(int $maximumLength): self
{
$this->maximumLength = $maximumLength;
return $this;
}
public function doNotGenerateSlugsOnCreate(): self
{
$this->generateSlugsOnCreate = false;
return $this;
}
public function doNotGenerateSlugsOnUpdate(): self
{
$this->generateSlugsOnUpdate = false;
return $this;
}
public function usingSeparator(string $separator): self
{
$this->slugSeparator = $separator;
return $this;
}
public function usingLanguage(string $language): self
{
$this->slugLanguage = $language;
return $this;
}
}
Hi @santino
The class SlugOptions that you are inspecting, just have all the functions needed for the user to configure the package.
If you want to understand where the package actually creates the slug, you should take a look at the HasSlug Trait:
https://github.com/spatie/laravel-sluggable/blob/master/src/HasSlug.php
When you tell your Model to use that Trait (as explained in package docs) you are making all the functions declared at that trait, available within your Model. It's basically the same as you were extending a base class: https://www.php.net/manual/en/language.oop5.traits.php
As you can see, the Trait registers a couple of Events Handlers at the bootHasSlug() method:
protected static function bootHasSlug()
{
static::creating(function (Model $model) {
$model->generateSlugOnCreate();
});
static::updating(function (Model $model) {
$model->generateSlugOnUpdate();
});
}
Those events are triggered when the Model gets created or updated: https://laravel.com/docs/8.x/eloquent#events
From there, you can keep inspecting the code to see what the package does when a model is created or updated and how it uses the options that you specified.
Finally, regarding the ::, those are static method calls:
https://www.php.net/manual/en/language.oop5.static.php
Hope this helps!
Please or to participate in this conversation.