diff --git a/app/Http/Controllers/CropFlowController.php b/app/Http/Controllers/CropFlowController.php index 4d7d130..befe5a4 100644 --- a/app/Http/Controllers/CropFlowController.php +++ b/app/Http/Controllers/CropFlowController.php @@ -16,7 +16,7 @@ class CropFlowController extends Controller $query = CropFlow::filter($request->input()); $totalNum = $query->sum('sale');//总产量 $list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)); - $list->load(['user']); + $list->load(['createdBy']); return $this->json([ 'total' => $totalNum, 'list' => CropFlowResource::collection($list), @@ -30,7 +30,8 @@ class CropFlowController extends Controller return array_merge($item, [ 'time_year' => $request->input('time_year'), 'crop_id' => $request->input('crop_id'), - 'user_id' => auth('api')->user()?->id ?? 0, + 'created_by' => auth('api')->user()?->id ?? 0, + 'updated_by' => auth('api')->user()?->id ?? 0, 'created_at' => now(), 'updated_at' => now(), ]); @@ -46,7 +47,9 @@ class CropFlowController extends Controller public function update(CropFlow $cropFlow, CropFlowUpdateRequest $request) { - $cropFlow->update($request->input()); + $cropFlow->update(array_merge($request->input(), + ['updated_by'=>auth('api')->user()?->id ?? 0,] + )); return $this->success('修改成功'); } diff --git a/app/Http/Controllers/CropYieldController.php b/app/Http/Controllers/CropYieldController.php index 33981e8..bf76c81 100644 --- a/app/Http/Controllers/CropYieldController.php +++ b/app/Http/Controllers/CropYieldController.php @@ -16,7 +16,7 @@ class CropYieldController extends Controller $query = CropYield::filter($request->all()); $totalNum = $query->sum('yield');//总产量 $list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)); - $list->load(['base', 'user']); + $list->load(['base', 'createdBy']); return $this->json([ 'total' => $totalNum, 'list' => CropYieldResource::collection($list) @@ -30,7 +30,8 @@ class CropYieldController extends Controller return array_merge($item, [ 'time_year' => $request->input('time_year'), 'crop_id' => $request->input('crop_id'), - 'user_id' => auth('api')->user()?->id ?? 0, + 'created_by' => auth('api')->user()?->id ?? 0, + 'updated_by' => auth('api')->user()?->id ?? 0, 'created_at' => now(), 'updated_at' => now(), ]); @@ -40,13 +41,15 @@ class CropYieldController extends Controller public function show(CropYield $cropYield) { - $cropYield->load(['base', 'crop', 'user']); + $cropYield->load(['base', 'crop', 'createdBy']); return $this->json(CropYieldResource::make($cropYield)); } public function update(CropYield $cropYield, CropYieldUpdateRequest $request) { - $cropYield->update($request->input()); + $cropYield->update(array_merge($request->input(), + ['updated_by'=>auth('api')->user()?->id ?? 0,] + )); return $this->success('修改成功'); } diff --git a/app/Http/Resources/CropFlowResource.php b/app/Http/Resources/CropFlowResource.php index d46c522..b489e26 100644 --- a/app/Http/Resources/CropFlowResource.php +++ b/app/Http/Resources/CropFlowResource.php @@ -23,8 +23,8 @@ class CropFlowResource extends JsonResource 'crop_id' => $this->crop_id, 'time_year' => $this->time_year, 'sale' => $this->sale, - 'user' => $this->whenLoaded('user', function (){ - return $this->user?->name; + 'created_by' => $this->whenLoaded('createdBy', function (){ + return $this->createdBy?->name; }, ''),//录入人 'created_at' => strtotime($this->created_at) ?? 0,//录入时间 ]; diff --git a/app/Http/Resources/CropYieldResource.php b/app/Http/Resources/CropYieldResource.php index e2bcf95..4b65c8d 100644 --- a/app/Http/Resources/CropYieldResource.php +++ b/app/Http/Resources/CropYieldResource.php @@ -28,8 +28,8 @@ class CropYieldResource extends JsonResource 'yield' => $this->yield, 'output' => $this->output, 'cultivated' => $this->cultivated, - 'user' => $this->whenLoaded('user', function (){ - return $this->user?->name; + 'created_by' => $this->whenLoaded('createdBy', function (){ + return $this->createdBy?->name; }, ''),//录入人 'created_at' => strtotime($this->created_at) ?? 0,//录入时间 ]; diff --git a/app/Models/CropFlow.php b/app/Models/CropFlow.php index 0360f78..6f94bdb 100644 --- a/app/Models/CropFlow.php +++ b/app/Models/CropFlow.php @@ -11,7 +11,8 @@ class CropFlow extends Model use Filterable; protected $fillable = [ - 'crop_id', 'flow_name', 'time_year', 'sale', 'user_id' + 'crop_id', 'flow_name', 'time_year', 'sale', 'user_id', + 'created_by', 'updated_by' ]; /** @@ -23,12 +24,13 @@ class CropFlow extends Model return $this->belongsTo(Keywords::class, 'crop_id'); } - /** - * 录入人 - * - * @return void - */ - public function user(){ - return $this->belongsTo(AdminUser::class, 'user_id'); + public function createdBy() + { + return $this->belongsTo(AdminUser::class, 'created_by'); + } + + public function updatedBy() + { + return $this->belongsTo(AdminUser::class, 'updated_by'); } } diff --git a/app/Models/CropYield.php b/app/Models/CropYield.php index 4897a5d..d272c27 100644 --- a/app/Models/CropYield.php +++ b/app/Models/CropYield.php @@ -10,7 +10,10 @@ class CropYield extends Model { use Filterable; - protected $fillable = ['base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id']; + protected $fillable = [ + 'base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id', + 'created_by', 'updated_by' + ]; /** * 基地 @@ -30,13 +33,14 @@ class CropYield extends Model return $this->belongsTo(Keywords::class, 'crop_id'); } - /** - * 录入人 - * - * @return void - */ - public function user(){ - return $this->belongsTo(AdminUser::class, 'user_id'); + public function createdBy() + { + return $this->belongsTo(AdminUser::class, 'created_by'); + } + + public function updatedBy() + { + return $this->belongsTo(AdminUser::class, 'updated_by'); } } diff --git a/database/migrations/2022_10_17_111601_create_crop_yields_table.php b/database/migrations/2022_10_17_111601_create_crop_yields_table.php index 2a8b3a1..708c204 100644 --- a/database/migrations/2022_10_17_111601_create_crop_yields_table.php +++ b/database/migrations/2022_10_17_111601_create_crop_yields_table.php @@ -21,7 +21,8 @@ return new class extends Migration $table->unsignedDecimal('yield', 12, 2)->comment('产量(斤)'); $table->unsignedDecimal('cultivated', 12, 2)->comment('耕地面积(亩)'); $table->unsignedDecimal('output', 12, 2)->comment('产值(元)'); - $table->unsignedBigInteger('user_id')->comment('录入人'); + $table->unsignedBigInteger('created_by')->comment('创建人ID'); + $table->unsignedBigInteger('updated_by')->comment('修改人ID'); $table->timestamps(); $table->comment('农产品产量'); diff --git a/database/migrations/2022_10_17_145621_create_crop_flows_table.php b/database/migrations/2022_10_17_145621_create_crop_flows_table.php index 9bbd1f2..63178f4 100644 --- a/database/migrations/2022_10_17_145621_create_crop_flows_table.php +++ b/database/migrations/2022_10_17_145621_create_crop_flows_table.php @@ -19,7 +19,8 @@ return new class extends Migration $table->string('flow_name')->comment('流向地'); $table->unsignedInteger('time_year')->comment('年份'); $table->unsignedDecimal('sale', 12, 2)->comment('销量(斤)'); - $table->unsignedBigInteger('user_id')->comment('录入人'); + $table->unsignedBigInteger('created_by')->comment('创建人ID'); + $table->unsignedBigInteger('updated_by')->comment('修改人ID'); $table->timestamps(); }); }