144 lines
3.1 KiB
PHP
144 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
/**
|
||
* 車種区分モデル - psectionテーブル
|
||
*
|
||
* 駐輪場の車種区分マスタデータを管理
|
||
*/
|
||
class Psection extends Model
|
||
{
|
||
/**
|
||
* テーブル名
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $table = 'psection';
|
||
|
||
/**
|
||
* プライマリキー
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $primaryKey = 'psection_id';
|
||
|
||
/**
|
||
* 一括代入可能な属性
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $fillable = [
|
||
'psection_subject', // 車種区分名
|
||
'operator_id', // オペレータID
|
||
'created_at', // 作成日時
|
||
'updated_at', // 更新日時
|
||
'psection_id' // 車種区分ID
|
||
];
|
||
|
||
|
||
|
||
/**
|
||
* キャストする属性
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $casts = [
|
||
'psection_id' => 'integer',
|
||
'operator_id' => 'integer',
|
||
'created_at' => 'datetime',
|
||
'updated_at' => 'datetime'
|
||
];
|
||
|
||
|
||
// 主キーが自動増分でない場合はfalseに設定
|
||
public $incrementing = false;
|
||
// タイムスタンプ管理しない場合はfalseに設定
|
||
public $timestamps = false;
|
||
|
||
|
||
|
||
/**
|
||
* 売上集計との関連
|
||
*
|
||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||
*/
|
||
public function earningsSummaries()
|
||
{
|
||
return $this->hasMany(EarningsSummary::class, 'psection_id', 'psection_id');
|
||
}
|
||
|
||
/**
|
||
* 定期契約との関連
|
||
*
|
||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||
*/
|
||
public function regularContracts()
|
||
{
|
||
return $this->hasMany(RegularContract::class, 'psection_id', 'psection_id');
|
||
}
|
||
|
||
/**
|
||
* アクティブな車種区分一覧を取得
|
||
*
|
||
* @return \Illuminate\Database\Eloquent\Collection
|
||
*/
|
||
public static function getActivePsections()
|
||
{
|
||
return self::orderBy('psection_id')->get();
|
||
}
|
||
|
||
/**
|
||
* 車種区分名で検索
|
||
*
|
||
* @param string $subject 車種区分名
|
||
* @return Psection|null
|
||
*/
|
||
public static function findBySubject(string $subject): ?Psection
|
||
{
|
||
return self::where('psection_subject', $subject)->first();
|
||
}
|
||
|
||
/**
|
||
* 文字列表現
|
||
*
|
||
* @return string
|
||
*/
|
||
public function __toString(): string
|
||
{
|
||
return sprintf(
|
||
'Psection[ID:%d, Subject:%s]',
|
||
$this->psection_id,
|
||
$this->psection_subject
|
||
);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// 新規作成時にoperator_idを自動設定(operator_idカラムがある場合のみ)
|
||
public static function boot()
|
||
{
|
||
parent::boot();
|
||
self::creating(function (Psection $model) {
|
||
// ログインしている場合のみセット
|
||
if (\Auth::check()) {
|
||
$model->operator_id = Auth::user()->ope_id;
|
||
}
|
||
});
|
||
}
|
||
|
||
// 車種区分リストを取得(プルダウン用)
|
||
public static function getList()
|
||
{
|
||
return self::orderBy('psection_id')->pluck('psection_subject', 'psection_id');
|
||
}
|
||
}
|