140 lines
3.8 KiB
PHP
140 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Facades\Auth;
|
||
|
||
class Price extends Model
|
||
{
|
||
const CREATED_AT = 'created_at';
|
||
const UPDATED_AT = 'updated_at';
|
||
const PRICE_MONTH = [
|
||
1 => '1ヶ月',
|
||
2 => '2ヶ月',
|
||
3 => '3ヶ月',
|
||
4 => '6ヶ月',
|
||
5 => '12ヶ月',
|
||
];
|
||
|
||
protected $table = 'price';
|
||
protected $primaryKey = 'price_parkplaceid';
|
||
|
||
protected $fillable = [
|
||
'price_parkplaceid',
|
||
'prine_name',
|
||
'price_month',
|
||
'park_id',
|
||
'psection_id',
|
||
'price_ptypeid',
|
||
'user_categoryid',
|
||
'pplace_id',
|
||
'price'
|
||
];
|
||
|
||
public static function boot()
|
||
{
|
||
parent::boot();
|
||
self::creating(function (Price $model) {
|
||
$model->operator_id = Auth::user()->ope_id;
|
||
});
|
||
}
|
||
|
||
public static function search($inputs)
|
||
{
|
||
$query = self::query()
|
||
->select(
|
||
'price.*',
|
||
\DB::raw("CONCAT_WS('/', usertype.usertype_subject1, usertype.usertype_subject2, usertype.usertype_subject3) as user_category_name"),
|
||
'psection.psection_subject',
|
||
'ptype.ptype_subject'
|
||
)
|
||
->leftJoin('usertype', 'price.user_categoryid', '=', 'usertype.user_categoryid')
|
||
->leftJoin('psection', 'price.psection_id', '=', 'psection.psection_id')
|
||
->leftJoin('ptype', 'price.price_ptypeid', '=', 'ptype.ptype_id');
|
||
|
||
// ソート対象カラム
|
||
$allowedSortColumns = [
|
||
'price_parkplaceid', // 駐車場所ID
|
||
'park_id', // 駐輪場ID
|
||
'prine_name', // 商品名
|
||
'price_month', // 期間
|
||
'user_categoryid', // 利用者分類ID
|
||
'price', // 駐輪料金(税込)
|
||
'psection_id', // 車種区分ID
|
||
'price_ptypeid', // 駐輪分類ID
|
||
'pplace_id', // 駐車車室ID
|
||
];
|
||
|
||
$sortColumn = $inputs['sort'] ?? '';
|
||
$sortType = strtolower($inputs['sort_type'] ?? 'asc');
|
||
|
||
if (in_array($sortColumn, $allowedSortColumns, true)) {
|
||
if (!in_array($sortType, ['asc', 'desc'], true)) {
|
||
$sortType = 'asc';
|
||
}
|
||
$query->orderBy($sortColumn, $sortType);
|
||
}
|
||
|
||
return $inputs['isExport']
|
||
? $query->get()
|
||
: $query->paginate(\App\Utils::item_per_page ?? 20);
|
||
}
|
||
|
||
|
||
|
||
|
||
public static function getByPk($pk)
|
||
{
|
||
return self::find($pk);
|
||
}
|
||
|
||
public static function deleteByPk($arr)
|
||
{
|
||
return self::whereIn('price_parkplaceid', $arr)->delete();
|
||
}
|
||
|
||
//TODO 駐車場所ID not found in database specs
|
||
|
||
//TODO 駐車車室ID not found in database specs
|
||
|
||
public function getPark()
|
||
{
|
||
return $this->belongsTo(Park::class, 'park_id', 'park_id')->first();
|
||
}
|
||
public function getPSection()
|
||
{
|
||
return $this->belongsTo(Psection::class, 'psection_id', 'psection_id')->first();
|
||
}
|
||
public function getPType()
|
||
{
|
||
return $this->belongsTo(Ptype::class, 'price_ptypeid', 'ptype_id')->first();
|
||
}
|
||
public function getUserType()
|
||
{
|
||
return $this->belongsTo(Usertype::class, 'user_categoryid', 'user_categoryid')->first();
|
||
}
|
||
|
||
public function psection()
|
||
{
|
||
return $this->belongsTo(Psection::class, 'psection_id'); // 外部キーが psection_id
|
||
|
||
}
|
||
public function ptype()
|
||
{
|
||
return $this->belongsTo(Ptype::class, 'price_ptypeid'); // 外部キーが price_ptypeid
|
||
|
||
}
|
||
public function pplace()
|
||
{
|
||
return $this->belongsTo(Pplace::class, 'pplace_id'); // 外部キーが pplace_id
|
||
|
||
}
|
||
// public function getStation()
|
||
// {
|
||
// return $this->belongsTo(Station::class, 'price_parkplaceid', 'park_id');
|
||
// }
|
||
|
||
|
||
|
||
} |