fanatic/app/Models/Owned.php

62 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2022-04-27 18:57:21 -07:00
use App\Traits\Categorizable;
use App\Traits\Imageable;
use App\Traits\Ownable;
2022-04-28 12:26:55 -07:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Owned extends Model
{
use HasFactory;
2022-04-27 18:57:21 -07:00
use Ownable;
use Categorizable;
use Imageable;
protected $table = 'owned';
2022-04-27 18:57:21 -07:00
2022-04-28 12:01:17 -07:00
protected $casts = [
'opened' => 'datetime',
'hold_member_updates' => 'boolean',
];
2022-04-27 18:57:21 -07:00
/* ----------------------------------------------------------------------- relationships ---- */
2022-04-28 12:01:17 -07:00
// injected by trait: collective (belongsTo)
// injected by trait: categories (many-to-many polymorphic)
2022-04-28 12:26:55 -07:00
/* --------------------------------------------------------------------------------- url ---- */
protected function url() : Attribute
{
return Attribute::make(
get: fn () => "/{$this->slug}",
);
}
2022-04-28 12:01:17 -07:00
/* ------------------------------------------------------------------------------- store ---- */
public static function store(array $validated) : static
{
$validated['image'] = $validated['image'] ?? null;
$owned = new static();
$owned->subject = $validated['subject'];
$owned->status = $validated['status'];
$owned->slug = $validated['slug'];
$owned->title = $validated['title'] ?? null;
$owned->image = self::imagePath($validated['image']);
$owned->opened = $validated['opened'] ?? null;
$owned->hold_member_updates = $validated['hold_member_updates'] ?? false;
auth_collective()->owned()->save($owned);
$owned->categories()->sync($validated['categories']);
return $owned;
}
}