80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class Article extends Model
|
|
{
|
|
use HasFactory;
|
|
use SoftDeletes;
|
|
|
|
/** The name of the database table storing this Model's records. */
|
|
protected $table = 'articles';
|
|
|
|
/** The attributes on the Model that can be mass-assigned */
|
|
protected $fillable = [
|
|
'title',
|
|
'subtitle',
|
|
'author_id',
|
|
'slug',
|
|
'body',
|
|
'published_at',
|
|
'header_image_url',
|
|
'markdown_body',
|
|
'processed_at',
|
|
'subscribers_notified_at',
|
|
];
|
|
|
|
/** ---------------------------------------
|
|
* ACCESSORS
|
|
* ---------------------------------------
|
|
*/
|
|
|
|
protected function publishedAt(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (?string $value) => $value ? Carbon::parse($value)->format('D, M jS o, g:i A') : null,
|
|
);
|
|
}
|
|
|
|
protected function publishedAtRaw(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (?string $value, array $attributes) => $attributes['published_at'],
|
|
);
|
|
}
|
|
|
|
/** ---------------------------------------
|
|
* RELATIONSHIPS
|
|
* ---------------------------------------
|
|
*/
|
|
|
|
/** The person that wrote the article. */
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Author::class);
|
|
}
|
|
|
|
/** ---------------------------------------
|
|
* MODEL METHODS
|
|
* ---------------------------------------
|
|
*/
|
|
|
|
public function convertMarkdownToMarkup()
|
|
{
|
|
if (is_null($this->markdown_body)) return null;
|
|
|
|
$this->body = Str::of($this->markdown_body)->markdown([
|
|
'html_input' => 'strip',
|
|
'allow_unsafe_links' => false,
|
|
]);
|
|
}
|
|
}
|