91 lines
1.7 KiB
PHP
91 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Models\Category;
|
|
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Source;
|
|
use App\Topic;
|
|
use Laravel\Scout\Searchable;
|
|
|
|
class Article extends Model
|
|
{
|
|
use Uuid;
|
|
use Searchable;
|
|
|
|
/**
|
|
* The "type" of the auto-incrementing ID.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $keyType = 'string';
|
|
|
|
/**
|
|
* Indicates if the IDs are auto-incrementing.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $incrementing = false;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guarded = [];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = [
|
|
'published_date',
|
|
];
|
|
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'body' => 'array',
|
|
'meta' => 'array'
|
|
];
|
|
|
|
public function source()
|
|
{
|
|
return $this->belongsTo(Source::class);
|
|
}
|
|
|
|
public function topics()
|
|
{
|
|
return $this->belongsToMany(Topic::class)->withTimestamps();
|
|
}
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function ScopeRecentArticles()
|
|
{
|
|
return $this->with('source', 'topics')
|
|
->latest("published_date");
|
|
}
|
|
|
|
public function toSearchableArray()
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'title' => $this->title,
|
|
'body' => implode(" ",$this->body),
|
|
'author' => $this->author,
|
|
'source' => $this->source()->get(),
|
|
'topics' => $this->topics()->get()
|
|
];
|
|
}
|
|
}
|