102 lines
2.6 KiB
PHP
102 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Feed\Feedable;
|
|
use Spatie\Feed\FeedItem;
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
class Home extends Model implements Feedable
|
|
{
|
|
protected $fillable = [
|
|
'title', 'price', 'surface', 'garden_surface', 'rooms', 'energy',
|
|
'ges', 'description', 'city', 'pictures', 'map', 'url', 'pictures_downloaded',
|
|
];
|
|
|
|
protected $casts = [
|
|
'map' => 'array',
|
|
'pictures' => 'array',
|
|
];
|
|
|
|
public function excerpt()
|
|
{
|
|
return Str::words($this->description, 10);
|
|
}
|
|
|
|
public function scopeComment(QueryBuilder $query, $comment): QueryBuilder
|
|
{
|
|
return $query
|
|
->where('comment', 'LIKE', '%'.$comment.'%')
|
|
->orWhere('description', 'LIKE', '%'.$comment.'%');
|
|
}
|
|
|
|
public function scopePriceMax(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('price', '<=', $price);
|
|
}
|
|
|
|
public function scopePriceMin(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('price', '>=', $price);
|
|
}
|
|
|
|
public function scopeSurfaceMax(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('surface', '<=', $price);
|
|
}
|
|
|
|
public function scopeSurfaceMin(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('surface', '>=', $price);
|
|
}
|
|
|
|
public function scopeGardenSurfaceMax(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('garden_surface', '<=', $price);
|
|
}
|
|
|
|
public function scopeGardenSurfaceMin(QueryBuilder $query, $price): QueryBuilder
|
|
{
|
|
return $query->where('garden_surface', '>=', $price);
|
|
}
|
|
|
|
/**
|
|
* The "booted" method of the model.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// Download images on creation
|
|
static::created(static function ($home) {
|
|
if (!empty($home->pictures)) {
|
|
Artisan::call('app:downloadimage ' . $home->id);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function toFeedItem()
|
|
{
|
|
return FeedItem::create()
|
|
->id($this->id)
|
|
->title($this->title)
|
|
->summary($this->excerpt())
|
|
->author('Shikiryu')
|
|
->updated($this->created_at)
|
|
->link(\route('public.view', ['slug' => $this->slug]))
|
|
;
|
|
}
|
|
|
|
public static function getFeedItems()
|
|
{
|
|
return self::where('slug', '!=', null)->limit(20)->get();
|
|
}
|
|
}
|