File: D:/HostingSpaces/SBogers110/franciscaansebeweging.nl/app/Komma/Travels/Models/Travel.php
<?php
namespace Komma\Travels\Models;
use Carbon\Carbon;
class Travel extends \Komma\Kms\Travels\Models\Travel
{
public $kmsClass = 'Komma\\Kms\\Travels\\Models\\Travel';
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function page(){
return $this->hasOne('Komma\Pages\Models\Page');
}
/**
* Get the translation for the current language
*
* @return Collection
*/
public function translation()
{
/**
* On the translation model is an BelongsTo relation.
* We want to collect the current translation.
* Therefore we create a hasOne relation
* Where we will select the page_translation,
* Join on the languages table
* And set the current Locale as the languages.iso_2
*
*/
return $this->hasOne('Komma\Travels\Models\TravelTranslation')
//We only need the translation
->select('travel_translations.*')
//Join the languages
->join('languages', 'languages.id', '=', 'travel_translations.language_id')
//Get only the language with the correct lang
->where('languages.iso_2', '=', \App::getLocale());
}
/**
* Get all translations
*
* @return Collection
*/
public function allTranslations(){
return $this->hasMany('Komma\Travels\Models\TravelTranslation')
//Join the languages
->select('travel_translations.*', 'languages.iso_2')
->join('languages', 'languages.id', '=', 'travel_translations.language_id')
//We only need the translation and language iso
->whereIn('languages.iso_2', \Config::get('app.availableLanguages'))
->orderBy('languages.id');
}
/**
* Get the images from the current page
*
* @return Collection
*/
public function images()
{
/**
*
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageble_type is filled in with the KmsClass
* And the imageble_id is set as the foreign_id,
* we can collect the images of the given model directly.
*
*/
return $this->hasMany('Komma\Kms\Images\Models\Image', 'imageble_id')
->where('imageble_type', '=', $this->kmsClass)
->orderBy('sort_order');
}
/**
*
* @return Carbon date
*/
public function getStartDate()
{
$date = $this->start_date;
$date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
return $date;
}
/**
*
* @return Carbon date
*/
public function getEndDate()
{
$date = $this->end_date;
$date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
return $date;
}
/**
*
* @return integer day duration
*/
public function getDuration()
{
$start_date = $this->getStartDate();
$end_date = $this->getEndDate();
return ($start_date->diffInDays($end_date) + 1);
}
}