/** * 作成画面 * * @return \Illuminate\View\View */ public function create() { // 作物一覧を取得 $crops = Crop::orderBy('display_order')->get(); // 編集中の日誌がない圃場一覧を取得 $workFields = WorkField::doesntHaveActiveDiary()->orderBy('display_order')->get(); return view('workDiary.create', compact('crops', 'workFields')); }
/** * Run the migrations. * * @return void */ public function up() { Schema::create('crops', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $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 (['スイカ', 'ほうれん草', '小松菜'] as $name) { $crop = new Crop(); $crop->name = $name; $crop->display_order = $order++; $crop->save(); } }
/** * 作成画面 * * @param Request $request * @return \Illuminate\View\View */ public function create(Request $request) { // 農薬情報をクリア session()->forget('workRecord.pesticides'); // 作物一覧を取得 $crops = Crop::orderBy('display_order')->get(); // 未選択の場合、先頭の作物を選択 $crop = Crop::findOrFail($request->input('crop_id') ?: $crops->first()->id); // 編集中の日誌がある作業日誌を取得 $workDiaries = WorkDiary::with('workField')->where('archive', false)->where('crop_id', $crop->id)->get()->sortBy('workField.display_order'); // 作物に紐付く作業内容を取得 $works = $crop->works()->orderBy('works.display_order')->get(); // 品種を取得 $cultivars = $crop->cultivars()->get(); // 農薬情報を取得 $pesticides = $crop->pesticides()->with('unit')->get(); if ($request->ajax()) { return compact('workDiaries', 'works', 'cultivars', 'pesticides'); } else { return view('workRecord.create', compact('crops', 'workDiaries', 'works', 'cultivars', 'pesticides')); } }
public function insert($cropId, array $workIds) { \App\Models\Crop::find($cropId)->works()->attach($workIds); }
public function insert($cropId, array $pesticideIds) { \App\Models\Crop::find($cropId)->pesticides()->attach($pesticideIds); }