/** * Run the migrations. * * @return void */ public function up() { Schema::create('work_fields', function (Blueprint $table) { $table->increments('id'); $table->integer('group_id')->unsigned(); $table->foreign('group_id')->references('id')->on('groups')->onDelete('restrict'); $table->string('name'); $table->text('remarks')->nullable(); $table->integer('display_order')->unsigned(); $table->timestamps(); $table->softDeletes(); $table->unsignedInteger('created_user_id')->nullable(); $table->unsignedInteger('updated_user_id')->nullable(); $table->unsignedInteger('deleted_user_id')->nullable(); }); $order = 1; foreach (['A', 'B'] as $a) { foreach (range(1, 6) as $b) { $workField = new WorkField(); $workField->group_id = Group::GUEST; $workField->name = $a . $b; $workField->display_order = $order++; $workField->save(); } } }
/** * 一覧表示 * * @param Request $request * @return \Illuminate\View\View */ public function index(Request $request) { // 作物一覧を取得 $crops = Crop::orderBy('display_order')->get(); // 未選択の場合、先頭の作物を選択 $crop = Crop::findOrFail($request->input('crop_id') ?: $crops->first()->id); // 作物に紐付く作業内容を取得 $works = $crop->works()->orderBy('works.display_order')->get(); if ($request->ajax()) { return compact('works'); } // 圃場一覧を取得 $workFields = WorkField::orderBy('display_order')->get(); // 検索 $workRecordsQuery = WorkRecord::with(['workSeeding', 'workPestControls', 'workDiaries' => function ($query) use($request) { if (!$request->has('archive')) { $query->where('archive', false); } }, 'workDiaries.workField', 'work'])->whereExists(function ($query) use($request, $crop) { $query->select(DB::raw(1))->from('work_diaries')->join('work_diary_work_record', 'work_diary_work_record.work_diary_id', '=', 'work_diaries.id')->whereNull('work_diaries.deleted_at')->whereRaw('work_diary_work_record.work_record_id = work_records.id'); if (!$request->has('archive')) { $query->where('archive', false); } if ($request->has('field_ids')) { // 圃場を絞込 $query->whereIn('work_diaries.work_field_id', $request->input('field_ids')); } })->where('crop_id', $crop->id); if ($request->has('work_ids')) { // 作業内容を絞込 $workRecordsQuery->whereIn('work_id', $request->input('work_ids')); } $workRecords = $workRecordsQuery->latest()->paginate(config('const.max_work_record')); return view('workRecord.index', compact('crops', 'works', 'workFields', 'workRecords')); }
/** * 作成処理 * * @param WorkDiaryStoreRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(WorkDiaryStoreRequest $request) { $errors = new MessageBag(); DB::transaction(function () use($request, &$errors) { $fieldIds = (array) $request->input('field_ids'); $workFields = WorkField::whereIn('id', $fieldIds)->lockForUpdate()->get(); if (!WorkField::whereIn('id', $fieldIds)->hasActiveDiary()->get()->isEmpty()) { // 編集中日誌のある圃場が選択されている $errors->add('field_ids', message('others_update')); DB::rollBack(); return; } foreach ($workFields as $workField) { // 日誌を作成 $workDiary = new WorkDiary(); $workDiary->crop_id = $request->get('crop_id'); $workDiary->work_field_id = $workField->id; $workDiary->archive = false; $workDiary->fill($request->all()); $workDiary->save(); } }); if ($errors->any()) { return $this->buildFailedValidationResponse($request, $errors->toArray()); } return redirect()->route('workDiary.index')->with('complete', 'store'); }