<?php

namespace App\Http\Resources;

use App\Models\Material;
use Illuminate\Http\Resources\Json\JsonResource;

class APiMaterialsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'material_no' => $this->material_no,
            'material_name' => $this->get_material_name($this->material_no),
            'unit_of_measure' => $this->unit_of_measure,
            'doc_no' => $this->doc_no
        ];
    }

    private function get_material_name($no)
    {
        $mat = Material::where('material_no', $no)->first();
        return $mat->material_name;
    }
}