fanatic/app/Models/Joined.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2022-04-23 16:25:16 -07:00
<?php
namespace App\Models;
use App\Traits\Categorizable;
2022-04-26 18:43:36 -07:00
use App\Traits\Imageable;
2022-04-27 18:57:21 -07:00
use App\Traits\Ownable;
2022-04-23 16:25:16 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Joined extends Model
{
2022-04-26 18:43:36 -07:00
use HasFactory;
2022-04-27 18:57:21 -07:00
use Ownable;
2022-04-26 18:43:36 -07:00
use Categorizable;
use Imageable;
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
protected $table = 'joined';
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
protected $fillable = [
'url',
'subject',
'approved',
'image',
];
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
protected $casts = [
'approved' => 'boolean',
];
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
/* ----------------------------------------------------------------------- relationships ---- */
2022-04-23 16:25:16 -07:00
2022-04-27 18:57:21 -07:00
// injected by trait: collective (belongsTo)
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
// injected by trait: categories (morph many-to-many)
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
/* ------------------------------------------------------------------------------ joined ---- */
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
public static function store(array $validated) : Joined
{
$validated['image'] = $validated['image'] ?? null;
2022-04-26 18:50:17 -07:00
$joined = new static();
$joined->url = $validated['url'];
$joined->subject = $validated['subject'];
$joined->image = self::imagePath($validated['image']);
$joined->approved = $validated['approved'] ?? false;
auth_collective()->joined()->save($joined);
2022-04-26 18:43:36 -07:00
$joined->categories()->sync($validated['categories']);
return $joined;
}
/* ------------------------------------------------------------------------------- patch ---- */
public function patch(array $validated) : Joined
{
$validated['image'] = $validated['image'] ?? null;
$this->url = $validated['url'];
$this->subject = $validated['subject'];
$this->image = $this->updateImage($validated['image']);
$this->approved = $validated['approved'] ?? false;
if ($this->isDirty()) {
$this->save();
}
if (isset($validated['categories'])) {
$this->categories()->sync($validated['categories']);
}
return $this;
}
2022-04-26 19:13:53 -07:00
/* ------------------------------------------------------------------------------ remove ---- */
public function remove() : void
{
$this->categories()->detach();
$this->delete();
}
2022-04-23 16:25:16 -07:00
}