<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Support\Facades\DB;

class Model extends EloquentModel
{
    public function scopeRecent($query)
    {
        return $query->orderBy('id', 'desc');
    }

    public function scopeOlder($query)
    {
        return $query->orderBy('id', 'asc');
    }

    public function scopeByUser($query, User $user)
    {
        return $query->where('user_id', $user->id);
    }

    public function batchInsert(array $data)
    {
        $time = date('Y-m-d H:i:s');
        foreach ($data as &$val) {
            $val['created_at'] = $val['created_at'] ?? $time;
            $val['updated_at'] = $val['updated_at'] ?? $time;
        }

        return Db::table($this->getTable())->insertOrIgnore($data);
    }
}
