dym-sh/app/Models/Article.php

46 lines
875 B
PHP
Raw Permalink Normal View History

2024-08-09 10:37:35 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
2024-08-09 13:52:23 +02:00
class Article extends Model
2024-08-09 10:37:35 +02:00
{
2024-08-09 13:52:23 +02:00
protected $table = 'articles';
2024-08-11 11:53:49 +02:00
protected $fillable = [ 'title', 'slug', 'body' ];
2024-08-09 10:37:35 +02:00
2024-08-09 13:52:23 +02:00
use HasFactory;
2024-08-09 10:37:35 +02:00
public static function find(string $slug): array
{
$article = Arr::first(Article::all(), fn($article) => $article['slug'] === $slug);
if(! $article ) {
abort(404);
}
return $article;
}
2024-08-11 11:53:49 +02:00
public static function findById(string $id): array
{
$article = Arr::first(Article::all(), fn($article) => $article['id'] === $id);
if(! $article ) {
abort(404);
}
return $article;
}
public function user_id()
{
return $this->belongsTo(User::class);
}
2024-08-09 10:37:35 +02:00
}