<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PointTable extends Model
{
	protected $fillable = [
		'years',
		'points',
		'total_points',
		'note',
		'created_by'
	];
	
	protected $casts = [
		'years'        => 'integer',
		'points'       => 'decimal:2',
		'total_points' => 'decimal:2'
	];
	
	public function creator()
	{
		return $this->belongsTo(User::class, 'created_by');
	}
	
	public function scopeForYears($query, $years)
	{
		return $query->where('years', '<=', $years)
			->orderBy('years', 'desc')
			->limit(1);
	}
}