86 lines
1.8 KiB
PHP
86 lines
1.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Cachet.
|
|
*
|
|
* (c) Alt Three Services Limited
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace CachetHQ\Cachet\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Tag extends Model
|
|
{
|
|
/**
|
|
* The attributes that should be casted to native types.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $casts = [
|
|
'name' => 'string',
|
|
];
|
|
|
|
/**
|
|
* The fillable properties.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = ['name'];
|
|
|
|
/**
|
|
* Overrides the models boot method.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
self::creating(function ($tag) {
|
|
$tag->slug = Str::slug($tag->name);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the components relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function components()
|
|
{
|
|
return $this->belongsToMany(Component::class);
|
|
}
|
|
|
|
/**
|
|
* @param array|\ArrayAccess $values
|
|
*
|
|
* @return \CachetHQ\Cachet\Models\Tag|static
|
|
*/
|
|
public static function findOrCreate($values)
|
|
{
|
|
$tags = collect($values)->map(function ($value) {
|
|
if ($value instanceof self) {
|
|
return $value;
|
|
}
|
|
|
|
$tag = static::where('name', '=', $value)->first();
|
|
|
|
if (!$tag instanceof self) {
|
|
$tag = static::create([
|
|
'name' => $value,
|
|
'slug' => Str::slug($value),
|
|
]);
|
|
}
|
|
|
|
return $tag;
|
|
});
|
|
|
|
return is_string($values) ? $tags->first() : $tags;
|
|
}
|
|
}
|