67 lines
1.7 KiB
PHP
67 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Database\Factories\ToolFactory;
|
||
|
|
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', 'logo_url', 'category', 'subcategory',
|
||
|
|
'tags', 'pricing', 'features', 'featured', 'approved', 'user_id',
|
||
|
|
])]
|
||
|
|
#[RouteKey('slug')]
|
||
|
|
class Tool extends Model
|
||
|
|
{
|
||
|
|
/** @use HasFactory<ToolFactory> */
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'tags' => 'array',
|
||
|
|
'features' => 'array',
|
||
|
|
'featured' => 'boolean',
|
||
|
|
'approved' => 'boolean',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
protected static function booted(): void
|
||
|
|
{
|
||
|
|
static::creating(function (Tool $tool) {
|
||
|
|
if (empty($tool->slug)) {
|
||
|
|
$tool->slug = Str::slug($tool->name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return BelongsTo<User, $this>
|
||
|
|
*/
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param Builder<Tool> $query
|
||
|
|
*/
|
||
|
|
#[Scope]
|
||
|
|
protected function approved(Builder $query): void
|
||
|
|
{
|
||
|
|
$query->where('approved', true);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function categoryLabel(): Attribute
|
||
|
|
{
|
||
|
|
return Attribute::get(fn () => config("tools.categories.{$this->category}", $this->category));
|
||
|
|
}
|
||
|
|
}
|