调整列表返回
parent
f03f175b5e
commit
2a22d31f4c
|
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
|||
use App\Helpers\Paginator;
|
||||
use App\Http\Requestes\CropYieldRequest;
|
||||
use App\Http\Resources\CropYieldResource;
|
||||
use App\Http\Resources\CropYieldListResource;
|
||||
use App\Models\AgriculturalBase;
|
||||
use App\Models\Crop;
|
||||
use App\Models\CropYield;
|
||||
|
|
@ -23,7 +24,7 @@ class CropYieldController extends Controller
|
|||
$list = $query->sort()->paginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||
$list->load(['base', 'crop', 'createdBy']);
|
||||
|
||||
return $this->json(CropYieldResource::collection($list));
|
||||
return $this->json(CropYieldListResource::collection($list));
|
||||
}
|
||||
|
||||
public function store(CropYieldRequest $request)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CropYieldListResource 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,
|
||||
'base_name' => $this->whenLoaded('base', function () {
|
||||
return $this->base?->name;
|
||||
}, ''),
|
||||
'base_id' => $this->base_id,
|
||||
'crop_name' => $this->whenLoaded('crop', function () {
|
||||
return $this->crop?->name;
|
||||
}, ''),
|
||||
'crop_id' => $this->crop_id,
|
||||
'time_year' => $this->time_year,
|
||||
'quarter' => $this->quarter,
|
||||
'yield' => $this->formatNumber($this->yield).$this->whenLoaded('crop', function () {
|
||||
return $this->crop?->unit;
|
||||
}, ''),
|
||||
'output' => $this->formatNumber($this->output),
|
||||
'cultivated' => $this->formatNumber($this->cultivated),
|
||||
'extends' => $this->extends,
|
||||
'created_by' => $this->whenLoaded('createdBy', function () {
|
||||
return $this->createdBy?->name;
|
||||
}, ''), //录入人
|
||||
'created_at' => strtotime($this->created_at) ?? 0, //录入时间
|
||||
];
|
||||
}
|
||||
|
||||
private function formatNumber($number){
|
||||
if($number > 10000){
|
||||
return round($number/10000, 2).'万';
|
||||
}else{
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue