Ana Maria
Ana Maria

Reputation: 25

Calculated column in Laravel to transform name in slug

I have a Location Model and Migration. Here I have name. Now I want to add an extra column (calculated), that will be name column with some replacement of special characters, and spaces to -. I will write that replace function by myself. I can't find a way of introducing calculated columns. Thanks.

Upvotes: 0

Views: 277

Answers (2)

Sameer Shaikh
Sameer Shaikh

Reputation: 7824

Add this to your model

protected $attributes = [
    'somecolumn'
];


public function getSomecolumnAttribute() {
    return str_slug($this->attributes['name'],'-');
}

Upvotes: 1

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

$title = str_slug("Laravel 5 Framework", "-");

//output: laravel-5-framework follow url for more helpers https://laravel.com/docs/5.0/helpers

Upvotes: 0

Related Questions