use App\Models\BaseModel;
use App\Models\FileAttachment;
use App\Models\Island;
use App\Models\Route;
use App\Models\State;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Howto extends BaseModel
{
protected $table = 'content_howtogetto';
protected $fillable = [
'title','title_eng','slug','slug_eng','content','content_eng',
'summary','summary_eng','location_id','is_featured','video','map_latitude','map_longtitude','publish_date',
'source','destination','direction','booking_url','booking_url_eng'
];
public $timestamps = true;
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
use SoftDeletes;
use State;
use Translation;
public static function boot()
{
parent::boot();
static::saving(function($post){
$post->slug = $post->slug ? mb_strtolower(str_replace(' ','-',$post->slug)) : mb_strtolower(str_replace(' ','-',
$post->slug));
$post->slug_eng = $post->slug_eng ? mb_strtolower(str_replace(' ','-',$post->slug_eng)) : mb_strtolower(str_replace('
','-',$post->slug_eng));
return $post;
});
}
public static function translatable()
{
return ['title','content','slug','summary','booking_url'];
}
public function featureImageTH()
{
return $this->morphOne(FileAttachment::class, 'attach')->where('content_type','feature_image');
}
public function featureImageEN()
{
return $this->morphOne(FileAttachment::class, 'attach')->where('content_type','feature_image_eng');
}
public function galleries()
{
return $this->morphMany(FileAttachment::class, 'attach')->where('content_type','gallery');
}
public function islands()
{
return $this->morphedByMany(Island::class ,
'related','post_relatables','page_id','related_id')->wherePivot('page_type','howto');
}
/*
* Scope
*/
public function scopeNewest($q)
{
return $q->orderBy('created_at', 'desc');
}
public function scopeOldest($q)
{
return $q->orderBy('created_at', 'asc');
}
public function scopeIsFeatured($q)
{
return $q->where('is_featured', true);
}
public function scopePublished($q)
{
return $q->where('publish_date','<=', now()); } public function scopeDraft($q) { return $q->where('publish_date','>=',
now());
}
public function scopeStatus($q){
$request = request();
if($request->only_publish){
return $q->published();
}elseif($request->only_trash){
return $q->onlyTrashed();
}elseif($request->only_draft){
return $q->draft();
}else{
return $q->withTrashed();
}
}
public function scopeSlugOrID($q,$slugOrID)
{
if($slugOrID){
return $q->where('slug', $slugOrID);
}else{
return $q->where('id', $slugOrID);
}
}
public static function relateCategoriesByID($post, $category)
{
$post_categories = $post->categories()->get()->pluck('name', 'id')->toArray();
$checked = count($post_categories) ? !empty($post_categories[$category]) ? 'checked' : '' : '';
return $checked;
}
public static function relateTagsByID($post, $tag)
{
$post_tags = $post->tags()->get()->pluck('name', 'id')->toArray();
$selected = count($post_tags) ? !empty($post_tags) ? 'selected' : '' : '';
return $selected;
}
public function getSourceNameAttribute()
{
switch($this->source):
case 'no' : $name = '-'; break;
case 'all' : $name = 'All'; break;
default :
$source_arr = explode('||', $this->source);
$name = Route::searchProvinceOrIsland($source_arr[0],$source_arr[1],'name' );
break;
endswitch;
return $name;
}
public function getDestinationNameAttribute()
{
switch($this->destination):
case 'no' : $name = '-'; break;
case 'all' : $name = 'All'; break;
default :
$source_arr = explode('||', $this->destination);
$name = Route::searchProvinceOrIsland($source_arr[0],$source_arr[1],'name' );
break;
endswitch;
return $name;
}
public function getSourceLocationAttribute()
{
if(!empty($this->source) && ($this->source !='all' && $this->source !='no') ){
$source_arr = explode('||', $this->source);
$result = Route::searchProvinceOrIsland($source_arr[0],$source_arr[1],'result' );
if($source_arr[1] == 'province' || empty($result->map_latitude)){
$location = $result->name;
}else{
$location = $result->map_latitude.','.$result->map_longtitude;
}
}else{
$location = 'Bangkok';
}
return $location;
}
public function getDestinationLocationAttribute()
{
if(!empty($this->destination) && ($this->destination !='all' && $this->destination !='no')){
$destination_arr = explode('||', $this->destination);
$result = Route::searchProvinceOrIsland($destination_arr[0],$destination_arr[1],'result' );
if($destination_arr[1] == 'province' || empty($result->map_latitude)){
$location = $result->name;
}else{
$location = $result->map_latitude.','.$result->map_longtitude;
}
}else{
$location = 'Bangkok';
}
return $location;
}
public function getCategoryListAttribute()
{
$categories = $this->categories()->get()->pluck('name', 'id');
return $categories;
}
public function getPublishDateFormatAttribute()
{
return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$this->publish_date);
}
public function getViewUrlAttribute()
{
if(!empty($this->slug)){
return route('admin.content.howto.show', $this->slug);
}
return route('admin.content.howto.show', $this->getKey());
}
public function getEditUrlAttribute()
{
return route('admin.content.howto.edit', $this->getKey());
}
public function getTrashUrlAttribute()
{
return route('admin.content.howto.trash', $this->getKey());
}
public function getDestroyUrlAttribute()
{
return route('admin.content.howto.destroy', $this->getKey());
}
public function getRestoreUrlAttribute()
{
return route('admin.content.howto.restore', $this->getKey());
}
public function scopeSlug($q,$slug)
{
$locale = app()->getLocale();
if($locale == 'th'){
$q->where('slug','like',$slug);
}else{
$q->where('slug_eng','like',$slug);
}
return $q;
}
}
Class "App\Models\Content\Howto\Howto" not found