<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\traits\SetConnection;
use Illuminate\Support\Facades\Auth;

class Lease extends Model
{
    protected $fillable = ['id', 'name', 'type', 'address', 'lat','lng', 'main_traffic','sub_traffic1','sub_traffic2','primary_school','secondary_school','age','area_val','floor','floor_type', 'point', 'comment',
        'memo', 'chinryo', 'kanrihi', 'shikikin', 'reikin', 'shikibiki', 'shohiyou', 'chukai', 'madori_url', 'order', 'display'];
    //
    use SetConnection;

    public static function getMaxNo() {
        $result = Lease::max('id');
        if($result == null){
            return 0;
        }
        return $result;
    }

    public static function getDataByCompany() {
        $result = Lease::orderby('order', 'ASC')->get();

        return $result;
    }

    public static function getData($cid) {
        $news = Lease::query();
        $result = $news->where('company_id', $cid)->where('disp', '=', 1)->orderby('created_at', 'desc')->get();

        return $result;
    }

    public function listImages()
    {
        return $this->hasMany(RelationLeasePhoto::class, "lease_id", "id")->orderByRaw("case when `order` is null then 1 else 0 end, `order`");
        //}
    }

}
