fanatic/app/Models/Joined.php

88 lines
2.3 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;
use Illuminate\Database\Eloquent\Casts\Attribute;
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;
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-26 18:43:36 -07:00
public function collective()
{
return $this->belongsTo(Collective::class);
}
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
/* -------------------------------------------------------------------------- attributes ---- */
2022-04-23 16:25:16 -07:00
2022-04-26 18:43:36 -07:00
protected function approved() : Attribute
{
return Attribute::make(
set: fn ($value) => isset($value) ? $value = $value : $value = false,
);
}
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-23 16:25:16 -07:00
}