Completed
Pull Request — master (#30)
by Sebastian
04:14
created

HasUrl::bootHasUrl()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace App\Foundation\Models\Traits;
4
5
trait HasUrl
6
{
7
    public static function bootHasUrl()
8
    {
9
        static::saving(function ($model) {
10
11
            if (!in_array('name', $model->translatable ?? [])) {
12
                $model->url = str_slug($model->name);
13
                return;
14
            }
15
16
            foreach ($model->getTranslatedLocales('name') as $locale) {
17
                $model->setTranslation('url', $locale, str_slug($model->getTranslation('name', $locale)));
18
            }
19
        });
20
    }
21
}
22