word-salad/app/Models/Article.php

70 lines
1.7 KiB
PHP
Raw Normal View History

<?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\Support\Carbon;
class Article extends Model
{
use HasFactory;
/** The name of the database table storing this Model's records. */
2024-08-04 04:49:42 +00:00
protected $table = 'articles';
/** The attributes on the Model that can be mass-assigned */
2024-08-04 04:49:42 +00:00
protected $fillable = [
'title',
'subtitle',
'author_id',
'slug',
'body',
'published_at',
'header_image_url',
'markdown_body',
'processed_at',
];
/** ---------------------------------------
* ACCESSORS
* ---------------------------------------
*/
protected function publishedAt(): Attribute
{
return Attribute::make(
get: fn (string $value) => Carbon::parse($value)->format('D, M jS o, g:i A'),
);
}
/** ---------------------------------------
* 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,
]);
}
}