HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/iphim.naniguide.com/app/Review.php
<?php

namespace App;

use Common\Search\Searchable;
use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $user_id
 * @property string body
 * @property integer reviewable_id
 * @property string reviewable_type
 * @method static Review findOrFail($id, $columns = ['*'])
 */
class Review extends Model
{
    use Searchable;

    const USER_REVIEW_TYPE = 'user';
    const MODEL_TYPE = 'review';
    protected $guarded = ['id'];
    protected $appends = ['media_type', 'model_type'];

    protected $casts = [
        'id' => 'integer',
        'user_id' => 'integer',
        'reviewable_id' => 'integer',
        'rating' => 'integer',
    ];

    public function getMediaTypeAttribute()
    {
        if ($this->attributes['reviewable_type'] === Episode::class) {
            return Episode::MODEL_TYPE;
        } else {
            return Title::MODEL_TYPE;
        }
    }

    public function user()
    {
        return $this->belongsTo(User::class)->select(
            'id',
            'first_name',
            'last_name',
            'email',
            'avatar',
        );
    }

    public function reviewable()
    {
        return $this->morphTo();
    }

    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'body' => $this->body,
            'created_at' => $this->created_at->timestamp ?? '_null',
            'updated_at' => $this->updated_at->timestamp ?? '_null',
        ];
    }

    public static function filterableFields(): array
    {
        return ['id', 'created_at', 'updated_at'];
    }

    public function toNormalizedArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'model_type' => self::MODEL_TYPE,
        ];
    }

    public static function getModelTypeAttribute(): string
    {
        return self::MODEL_TYPE;
    }
}