This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

phplee
phplee

I have to fields make,model and variant for a car leasing plugin i have written. i want the slug to have the format make_model_variant. how can i combine the 3 fields to generate thd blog

thanks

JeffGoldblum
JeffGoldblum

Easiest way to do this is in the beforeSave() method on your model.

class Car extends Model {
    use \October\Rain\Database\Traits\Sluggable;

    /**
     * @var array Generate slugs for these attributes.
     */
    protected $slugs = ['slug' => 'name'];

    public function beforeSave()
    {
        // Autogenerate name
        if (empty($this->name)) {
            $this->name = $this->make . ' ' . $this->model . ' ' . $this->variant;
        }

        // Force creation of slug
        if (empty($this->slug)) {
            unset($this->slug);
            $this->setSluggedValue('slug', 'name');
        }
    }
}

as an example that assumes you have a name property on the Car model and then generates a name out of the desired properties and generates a slug based on the name respecting potential duplicates within the database.

phplee
phplee

Thank you. That worked!

cleanse_
cleanse_

protected $slugs = ['slug' => ['make', 'model', 'variant']]; Also works (set the slug = to an array)

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.