public function index() { $sample = '<?php class Idea extends Eloquent { /** * Dreaming of something more? * * @with Laravel */ public function create() { // Have a fresh start... } }'; $artisans = User::verifiedOnly()->count(); $bins = Bin::publicOnly()->count(); $files = Snippet::publicOnly()->count(); return view('home', compact('sample', 'artisans', 'bins', 'files')); }
public function editPost(Bin $bin, Requests\Bins\CreateBin $request) { $description = $request->has('description') && trim($request->input('description')) != '' ? $request->input('description') : null; // Update the bin itself $bin->title = $request->input('title'); $bin->description = $description; $bin->visibility = $request->input('visibility'); $bin->save(); $bin->versions()->sync($request->input('versions')); // Files that currently exist with the bin object $existingFiles = []; foreach ($bin->snippets as $file) { $existingFiles[] = $file->getRouteKey(); } /** * Now we need to check that the existing files are still * present in the post, if they are then they are updated * if they are not, then they are deleted. */ $updatedExisting = []; $files = []; if ($request->has('file')) { foreach ($request->input('file') as $key => $value) { $files[$key]['file'] = $value; $updatedExisting[] = $value; } } foreach ($request->input('name') as $key => $value) { $files[$key]['name'] = $value; } foreach ($request->input('language') as $key => $value) { $files[$key]['language'] = $value; } foreach ($request->input('code') as $key => $value) { $files[$key]['code'] = $value; } $filesToDelete = []; foreach ($existingFiles as $key => $value) { if (!in_array($value, $updatedExisting)) { /** * This previously existing file was not * found in the update. User wants to delete it. */ $filesToDelete[] = hashid()->decode($value)[0]; } } Snippet::where('bin_id', $bin->id)->whereIn('id', $filesToDelete)->delete(); foreach ($files as $item) { $type = Type::where('css_class', $item['language'])->first(); if (array_key_exists('file', $item)) { // update existing snippet $snippet = Snippet::where(['bin_id' => $bin->id, 'id' => hashid()->decode($item['file'])])->first(); $snippet->type_id = $type->id; $snippet->name = $item['name']; $snippet->code = $item['code']; $snippet->save(); } else { // create new snippet $bin->snippets()->create(['type_id' => $type->id, 'name' => $item['name'], 'code' => $item['code']]); } } session()->flash('success', 'Bin updated successfully!'); return redirect()->route('bin.code', $bin->getRouteKey()); }