67 lines
1.7 KiB
PHP
67 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Database\Factories\SkillFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
|
|
use Illuminate\Database\Eloquent\Attributes\RouteKey;
|
||
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
#[Fillable([
|
||
|
|
'name', 'slug', 'description', 'url', 'docs_url', 'logo_url', 'category',
|
||
|
|
'install_instructions', 'example_prompt', 'requirements', 'tags',
|
||
|
|
'featured', 'approved', 'user_id',
|
||
|
|
])]
|
||
|
|
#[RouteKey('slug')]
|
||
|
|
class Skill extends Model
|
||
|
|
{
|
||
|
|
/** @use HasFactory<SkillFactory> */
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'tags' => 'array',
|
||
|
|
'featured' => 'boolean',
|
||
|
|
'approved' => 'boolean',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
protected static function booted(): void
|
||
|
|
{
|
||
|
|
static::creating(function (Skill $skill) {
|
||
|
|
if (empty($skill->slug)) {
|
||
|
|
$skill->slug = Str::slug($skill->name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return BelongsTo<User, $this>
|
||
|
|
*/
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param Builder<Skill> $query
|
||
|
|
*/
|
||
|
|
#[Scope]
|
||
|
|
protected function approved(Builder $query): void
|
||
|
|
{
|
||
|
|
$query->where('approved', true);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function categoryLabel(): Attribute
|
||
|
|
{
|
||
|
|
return Attribute::get(fn () => config("skills.categories.{$this->category}", $this->category));
|
||
|
|
}
|
||
|
|
}
|