Skip to content

Instantly share code, notes, and snippets.

@valex
Created December 21, 2018 13:34
Show Gist options
  • Save valex/13fd8aab1af653e4a32d78473aa1c3a1 to your computer and use it in GitHub Desktop.
Save valex/13fd8aab1af653e4a32d78473aa1c3a1 to your computer and use it in GitHub Desktop.
Laravel Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Station extends Model
{
const TYPE_LARGEST = 0; // важнейшие станции
const TYPE_MAIN = 1; // главные, узловые, конечные станции
const TYPE_LINE = 2; // линейные станции
const TYPE_PLATFORM = 3; // платформы, остановочные пункты
const TYPE_CROSSING_POINT = 4; // разъезд
const TYPE_POST = 5; // пост
protected $table = 'stations';
public $timestamps = false;
protected $fillable = [
'name',
'type',
'yandex_station_code',
'schedule_made_at',
'region_id',
'settlement_id',
'lat',
'lng',
'zoom',
'require_line_in_name',
];
protected $casts = [
'require_line_in_name' => 'boolean',
'schedule_made_at' => 'datetime',
];
public function region()
{
return $this->belongsTo('App\Region', 'region_id', 'id');
}
public function hasSettlement()
{
return ! is_null($this->settlement_id);
}
public function settlement()
{
return $this->belongsTo('App\Settlement', 'settlement_id', 'id');
}
public function settlementName(){
$name = null;
if($this->hasSettlement()){
$name = $this->settlement->name();
}
return $name;
}
public function lines()
{
return $this->belongsToMany('App\Line', 'line_station')
->withPivot('distance_marker');
}
public function schedule()
{
return $this->hasMany('App\Rasp', 'station_id', 'id');
}
public function latLng(){
return [
'lat' => $this->lat,
'lng' => $this->lng,
];
}
public function name($line = true){
$name = $this->name;
if(true === $line && true === $this->require_line_in_name){
$line = $this->lines()->first();
if( ! is_null($line)){
$name = $name.' ( '.$line->name().' )';
}
}
return $name;
}
public function typeName(){
switch ($this->type){
case self::TYPE_LARGEST:
return trans('main.large station');
break;
case \App\Station::TYPE_MAIN:
return trans('main.main station');
break;
case \App\Station::TYPE_LINE:
return trans('main.station');
break;
case \App\Station::TYPE_PLATFORM:
return trans('main.platform');
break;
case \App\Station::TYPE_CROSSING_POINT:
return trans('main.crossing point');
break;
case \App\Station::TYPE_POST:
return trans('main.post station');
break;
}
}
public function hasRasp(){
return ! is_null($this->yandex_station_code) &&
mb_strlen($this->yandex_station_code) > 0 &&
! is_null($this->schedule_made_at);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment