<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    protected $table = 'news';
    protected $primaryKey = 'news_id';

    protected $fillable = [
        'news_id',
        'news',
        'open_datetime',
        'link_url',
        'image1_filename',
        'image2_filename',
        'mode',
        'management_code',
        'created_at',
        'updated_at',
        'operator_id',
    ];

    protected $casts = [
        'news_id' => 'integer',
        'open_datetime' => 'datetime',
        'mode' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'operator_id' => 'integer',
        'management_code' => 'string',
    ];

    /**
     * ニュース情報の最新5件を取得
     */
    public static function getNews($management_code)
    {
        return self::where('management_code', $management_code)
            ->whereIn('mode', [2, 3])
            ->where('open_datetime', '<=', now())
            ->orderBy('open_datetime', 'desc')
            ->limit(5)
            ->get();
    }
} 
