1 | <?php |
||
2 | |||
3 | namespace Giuga\LaravelSeoMetaBox\Traits; |
||
4 | |||
5 | use Giuga\LaravelSeoMetaBox\Models\Seo; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | |||
8 | trait HasSeo |
||
9 | { |
||
10 | abstract public function getSeoOptions(): SeoOptions; |
||
11 | |||
12 | protected static function bootHasSeo() |
||
13 | { |
||
14 | static::saved(function (Model $model) { |
||
15 | $options = $model->getSeoOptions(); |
||
16 | $seo = Seo::firstOrNew([ |
||
17 | 'type' => get_class($model), |
||
18 | 'object_id' => $model->{$model->primaryKey}, |
||
19 | ]); |
||
20 | $seo->slug = $options->routePrefix ?? ''; |
||
21 | if ($options->hasSlug) { |
||
22 | $seo->slug .= $model->{$options->slugField}; |
||
23 | } else { |
||
24 | $seo->slug .= $model->{$model->primaryKey}; |
||
25 | } |
||
26 | if (($title = request()->get('seo_title', false)) !== false) { |
||
27 | $seo->title = $title; |
||
28 | } else { |
||
29 | if ((empty($seo->title) || $options->overwriteOnUpdate) && isset($options->titleField)) { |
||
30 | $seo->title = $model->{$options->titleField}; |
||
31 | } |
||
32 | } |
||
33 | if (($description = request()->get('seo_description', false)) !== false) { |
||
34 | $seo->description = $description; |
||
35 | } else { |
||
36 | if ((empty($seo->description) || $options->overwriteOnUpdate) && isset($options->descriptionField)) { |
||
37 | $seo->description = $model->{$options->descriptionField}; |
||
38 | } |
||
39 | } |
||
40 | $seo->save(); |
||
41 | }); |
||
42 | |||
43 | static::retrieved(function (Model $model) { |
||
44 | app()->make('laravel-seo-meta-box')->addObjectOnPage(get_class($model), $model->{$model->primaryKey}); |
||
45 | }); |
||
46 | } |
||
47 | |||
48 | public function seo() |
||
49 | { |
||
50 | return $this->hasOne(Seo::class, 'object_id')->where('type', static::class); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
51 | } |
||
52 | } |
||
53 |