<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BeritaModel extends Model
{
    use HasFactory;
    protected $table = 'berita';

    public static function allBerita()
    {
        return DB::table('berita')
            ->join('kategori', 'berita.id_kategori', '=', 'kategori.id_kategori')
            ->join('users', 'berita.id_user', '=', 'users.id')
            ->select('berita.*', 'kategori.*', 'users.name as nama_user')
            ->get();
    }

    public static function limit5()
    {
        return DB::table('berita')
            ->join('kategori', 'berita.id_kategori', '=', 'kategori.id_kategori')
            ->join('users', 'berita.id_user', '=', 'users.id')
            ->select('berita.*', 'kategori.*', 'users.name as nama_user')
            ->orderBy('berita.id_berita', 'desc')
            ->limit(5)
            ->get();
    }

    public static function find($id)
    {
        return DB::table('berita')
            ->join('kategori', 'berita.id_kategori', '=', 'kategori.id_kategori')
            ->join('users', 'berita.id_user', '=', 'users.id')
            ->select('berita.*', 'kategori.*', 'users.name as nama_user')
            ->where('berita.id_berita', '=', $id)
            ->first();
    }
}
