jonnybarnes.uk/app/Models/Tag.php

45 lines
1,015 B
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
declare(strict_types=1);
2017-12-19 16:00:42 +00:00
namespace App\Models;
2016-05-19 15:01:28 +01:00
2026-04-07 09:01:19 +01:00
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
2020-10-19 19:41:50 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2016-05-19 15:01:28 +01:00
use Illuminate\Database\Eloquent\Model;
2020-02-22 11:06:43 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2021-04-29 20:02:20 +01:00
use Illuminate\Support\Str;
2016-05-19 15:01:28 +01:00
2026-04-07 09:01:19 +01:00
#[Guarded(['id'])]
2016-05-19 15:01:28 +01:00
class Tag extends Model
{
2020-10-19 19:41:50 +01:00
use HasFactory;
2023-02-18 09:34:57 +00:00
public function notes(): BelongsToMany
2016-05-19 15:01:28 +01:00
{
return $this->belongsToMany(Note::class);
2016-05-19 15:01:28 +01:00
}
2023-02-18 09:34:57 +00:00
public function bookmarks(): BelongsToMany
2017-10-10 15:58:07 +01:00
{
2017-12-19 16:00:42 +00:00
return $this->belongsToMany('App\Models\Bookmark');
2017-10-10 15:58:07 +01:00
}
protected function tag(): Attribute
{
return Attribute::set(
set: fn ($value) => self::normalize($value),
);
}
/**
2023-02-18 09:34:57 +00:00
* Normalizes a tag.
*
* That means convert to lowercase and removing fancy diatric characters.
*/
public static function normalize(string $tag): string
{
2021-04-29 20:02:20 +01:00
return Str::slug($tag);
}
2016-05-19 15:01:28 +01:00
}