php - Laravel 5 ORM update request, like date_add -
i beg excuse me poor english. so, have laravel 5 orm, , need make request should add date rows, mysql date_add. input single date interval , array of id's, output rows of database, changed adding date interval. ideally, should 1 orm request. know possible use "bad" way , rows, update in code, , insert database, imho it's not good. hope answer link site or code if it's not complicate you. attention!
public function update($id) { $user_id = auth::user()->id; $rep_task = reptask::find($id); $cl_task = \request::has('json') ? json_decode(\request::input('json'),true) : \request::all(); $ids = []; $task_id = $rep_task->task_id; $rep_tasks = reptask::where('task_id', '=', $task_id) ->select('id') ->get(); $new_date = date_create_from_format(self::date_fullcalendar_format, $cl_task['new_date']); $selected_task_date = date_create_from_format(self::date_mysql_format, $rep_task->start_date); $diff = date_diff($selected_task_date, $new_date); if(\request::has('json')) { $ids = [1,2,3]; //this easy understanding db::table('rep_task') ->wherein('id', $ids) ->update(['start_date' => db::raw('date_add(start_date, interval ' . $diff->d . ' day)')]); $out_json = ['updated' => $ids]; return json_encode($out_json, json_unescaped_unicode); } else { $start_date = 0; $end_date = 0; if (!isset($cl_task['name']) || !isset($cl_task['text'])) return "{'error':'columns not defined'}"; if (isset($cl_task['start_date']) && isset($cl_task['end_date'])) { $dt = date_create_from_format(self::date_fullcalendar_format, $cl_task['start_date']); $start_date = $dt->format(self::date_mysql_format); $dt = date_create_from_format(self::date_fullcalendar_format,$cl_task['end_date']); $end_date = $dt->format(self::date_mysql_format); } $rep_task->name = $cl_task['name']; $rep_task->text = $cl_task['text']; $rep_task->start_date = $start_date; $rep_task->end_date = $end_date; $rep_task->users_id = $user_id; $rep_task->save(); } $user_id = auth::user()->id; $tasks = task::getalltasksbyuserfullcalendar($user_id); return view( 'task.index', [ 'tasks' => $tasks ] ); }
Comments
Post a Comment