Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Last active June 18, 2017 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzikbenh/cdd1e78c42392e9ce270d015eef1ebfd to your computer and use it in GitHub Desktop.
Save itzikbenh/cdd1e78c42392e9ce270d015eef1ebfd to your computer and use it in GitHub Desktop.
Laravel - project update with revision
<?php
public function update(Request $request, $id)
{
$this->validate($request, [
'number' => 'required',
'client_id' => 'required',
'location_id' => 'required',
'priority_id' => 'required',
'status_id' => 'required',
'date' => 'required',
], [
'client_id.required' => 'The client field is required.',
'location_id.required' => 'The location field is required.',
'priority_id.required' => 'The priority field is required.',
'status_id.required' => 'The status field is required.',
]);
$tenant = getCurrentTenant();
$project = $tenant->projects()->where('id', $id)->first();
//Using array diff to remove unwanted columns.
$before = array_diff_key($project->toArray(), array_flip(['created_at', 'updated_at', 'status_id', 'priority_id', 'client_id']));
$before['inventories'] = $project->inventories()->with(['material' => function ($query) { $query->select('id', 'type'); }])->get()->toArray();
$before['tags'] = $project->tags()->pluck('tag')->toArray();
$before['managers'] = $project->managers()->pluck('name')->toArray();
$before['locations'] = $project->locations()->pluck('location')->toArray();
$before['status'] = $project->status->status;
$before['priority'] = $project->priority->priority;
$before['client'] = $project->client->name;
$projectNum = $request->input('number');
if ($projectNum != $project->number) {
$projectNum = getUniqueNumber('App\Project', $projectNum);
$request->offsetSet('number', $projectNum);
}
$project->update($request->all());
$project->locations()->sync([$request->input('location_id')]);
if (! empty($request->input('manager_id'))) {
$project->managers()->sync([$request->input('manager_id')]);
} else {
$project->managers()->detach();
}
if (! empty($request->input('tag_id'))) {
$project->tags()->sync($request->input('tag_id'));
} else {
$project->tags()->detach();
}
$project->inventories()->delete();
$inventoryRows = count($request->input('inventory')['material_id']);
$inventories = [];
for ($i = 0; $i < $inventoryRows; $i++) {
$row = 'row-'.($i + 1);
$materialId = $request->input('inventory')['material_id'][$row][0];
$dateOrdered = $this->normalizeDate($request->input('inventory')['date_ordered'][$row][0]);
$estimatedArrival = $this->normalizeDate($request->input('inventory')['estimated_arrival'][$row][0]);
$isArrived = $request->input('inventory')['is_arrived'][$row][0] ?? 0;
if ($materialId) {
$inventories[] = [
'company_id' => $tenant->id,
'project_id' => $project->id,
'material_id' => $materialId,
'date_ordered' => $dateOrdered,
'estimated_arrival' => $estimatedArrival,
'is_arrived' => $isArrived
];
}
}
if (count($inventories)) {
DB::table('inventories')->insert($inventories);
}
$after = array_diff_key($project->toArray(), array_flip(['created_at', 'updated_at', 'status_id', 'priority_id', 'client_id']));
$after['inventories'] = $project->fresh()->inventories()->with(['material' => function ($query) { $query->select('id', 'type'); }])->get()->toArray();
$after['tags'] = $project->tags()->pluck('tag')->toArray();
$after['managers'] = $project->managers()->pluck('name')->toArray();
$after['locations'] = $project->locations()->pluck('location')->toArray();
$after['status'] = $project->fresh()->status->status;
$after['priority'] = $project->fresh()->priority->priority;
$after['client'] = $project->fresh()->client->name;
$changes = getChanges($before, $after);
if (count($changes)) {
$project->revisions()->create([
'company_id' => $tenant->id,
'user_id' => Auth::id(),
'revisionable_id' => $project->id,
'revisionable_type' => get_class($this),
'before' => $before,
'changes' => $changes,
'after' => $after,
]);
}
\Session::flash('flash_message', 'Project has been updated successfully');
return redirect()->route('projects.show', [$id]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment