<?php

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LaboratoriumResult extends Model
{
    use HasFactory;

    protected $table    = "laboratorium_result";
    protected $fillable = [
        'claim_id',
        'date',
        'location',
        'examination',
    ];

    protected $appends = ['lab_result_file'];
    protected $path_for_public = 'storage/lab_result';

    public function getLabResultFileAttribute()
    {
        $arr_files = [];
        $files = DB::table('files')->select('name','original_name','path','extension')->where("type","=","laboratorium-result")->where('fileable_id','=',$this->attributes['id'])->get();

        foreach ($files as $row) {
            $row->path = url($this->path_for_public . "/" . $row->original_name);

            $arr_files[] = [
                'lab_result_file_obj' => $row
            ];
        }

        return $arr_files;
    }
}
