Add (initial): futurewalker code

This commit is contained in:
2023-11-20 00:15:18 +08:00
parent f8602cb456
commit 9ce3e5c82a
166 changed files with 15941 additions and 1072 deletions

View File

@@ -7,6 +7,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
@@ -17,74 +18,93 @@
* Class Post
*
* @property int $id
* @property int|null $serp_url_id
* @property string|null $title
* @property string|null $slug
* @property string|null $type
* @property string|null $excerpt
* @property string|null $main_keyword
* @property string|null $keywords
* @property string|null $bites
* @property int|null $author_id
* @property bool $featured
* @property string|null $featured_image
* @property string|null $body
* @property int $views_count
* @property string $status
* @property Carbon|null $published_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property SerpUrl|null $serp_url
* @property Author|null $author
* @property Collection|PostCategory[] $post_categories
* @property Carbon $published_at
*/
class Post extends Model implements Feedable
{
protected $table = 'posts';
protected $casts = [
'serp_url_id' => 'int',
'author_id' => 'int',
'featured' => 'bool',
'views_count' => 'int',
'keywords' => 'array',
'published_at' => 'datetime',
'keywords' => 'array',
'metadata' => 'object',
];
protected $fillable = [
'serp_url_id',
'title',
'short_title',
'slug',
'type',
'excerpt',
'main_keyword',
'keywords',
'bites',
'author_id',
'featured',
'featured_image',
'body',
'views_count',
'status',
'main_keyword',
'keywords',
'published_at',
'metadata',
'society_impact',
'society_impact_level',
];
public function getFeaturedImageLqipCdnAttribute()
protected function featuredImage(): Attribute
{
if (! is_empty($this->featured_image)) {
// Get the extension of the original featured image
$extension = pathinfo($this->featured_image, PATHINFO_EXTENSION);
return Attribute::make(
get: function ($value = null) {
if (! is_empty($value)) {
return Storage::disk('r2')->url($value);
}
// Append "_lqip" before the extension to create the LQIP image URL
$lqipFeaturedImage = str_replace(".{$extension}", '_lqip.webp', $this->featured_image);
return null;
}
);
}
return 'https://'.Storage::disk('r2')->url($lqipFeaturedImage).'?a=bc';
protected function getFeaturedThumbImageAttribute()
{
$value = $this->featured_image;
//dd($value);
if (! is_empty($value)) {
// Extract the file extension
$extension = pathinfo($value, PATHINFO_EXTENSION);
// Construct the thumbnail filename by appending '_thumb' before the extension
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $value);
return $thumbnail;
// Return the full URL to the thumbnail image
//return Storage::disk('r2')->url($thumbnail);
}
return null;
}
public function getFeaturedImageCdnAttribute()
public function serp_url()
{
if (! is_empty($this->featured_image)) {
return 'https://'.Storage::disk('r2')->url($this->featured_image);
}
return null;
return $this->belongsTo(SerpUrl::class);
}
public function author()
@@ -92,6 +112,16 @@ public function author()
return $this->belongsTo(Author::class);
}
public function post_categories()
{
return $this->hasMany(PostCategory::class);
}
public function post_entities()
{
return $this->hasMany(PostEntity::class);
}
public function category()
{
return $this->hasOneThrough(
@@ -104,15 +134,27 @@ public function category()
);
}
public function entities()
{
return $this->hasManyThrough(
Entity::class, // The target model
PostEntity::class, // The through model
'post_id', // The foreign key on the through model
'id', // The local key on the parent model (Post)
'id', // The local key on the through model (PostEntity)
'entity_id' // The foreign key on the target model (Entity)
);
}
public function toFeedItem(): FeedItem
{
return FeedItem::create([
'id' => $this->id,
'title' => $this->title,
'summary' => $this->excerpt,
'summary' => $this->bites,
'updated' => $this->updated_at,
'link' => route('front.post', ['slug' => $this->slug, 'category_slug' => $this->category->slug]),
'authorName' => optional($this->author)->name,
'authorName' => 'FutureWalker',
]);
}