merge(['business_id' => $business])->except('_business_info')); // $statuses = collect($validatedData['statuses'])->map(function($status) use ($workflow, $business) { // $status['business_id'] = $business; // $status['workflow_id'] = $workflow->id; // return $status; // }); // $statuses = $workflow->statuses()->createMany($statuses->toArray()); return Business::info($business, true); } public function update($business, $workflow, Request $request) { permit('businessWorkFlows'); $workflowModel = Workflow::findOrFail($workflow); $workflowModel->update($request->except('_business_info')); return Business::info($business, true); } public function syncStatus($business, $workflowModel) { $old_statuses_name = array_keys(collect(\request('_business_info')['workflows'][$workflowModel->id]['statuses'])->keyBy('name')->toArray()); $new_statuses_name = array_keys(collect(\request('statuses'))->keyBy('name')->toArray()); $removed_statuses_name = array_diff(array_merge($old_statuses_name, $new_statuses_name), $new_statuses_name); foreach ($removed_statuses_name as $status_name) { //delete all statuses that removed name's from request->statuses $workflowModel->statuses()->where('name', $status_name)->first()->delete(); } foreach (request('statuses') as $status) { //sync another statuses $workflowModel->statuses() ->updateOrCreate( ['name' => $status['name'], 'business_id' => $business, 'workflow_id' => $workflowModel->id], ['state' => $status['state'], 'order' => $status['order']] ); } return $workflowModel; } public function delete($business, $workflow) { permit('businessWorkFlows'); $workflow = Workflow::findOrFail($workflow); foreach ($workflow->statuses as $status) { //delete all statuses related to this workflow $status->delete(); } $workflow->delete(); return Business::info($business, true); } }