/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { $router->bind('bin', function ($value) { $id = hashid()->decode($value); if (empty($id)) { $id = null; } else { $id = $id[0]; } return Bin::where('id', $id)->first(); }); $router->bind('comment', function ($value) { $id = hashid()->decode($value); if (empty($id)) { $id = null; } else { $id = $id[0]; } return Comment::where('id', $id)->first(); }); parent::boot($router); }
public function ajax(Request $request) { $type = $request->input('type'); $id = $request->input('id'); $record = Bin::where(['id' => hashid()->decode($id), 'user_id' => auth()->user()->getAuthIdentifier()])->first(); if (!$record) { return response()->json(['msg' => 'There was an issue with your request!'], 422); } if ($type == 'visibility') { $visibility = $request->input('visibility'); $record->visibility = $visibility; $record->save(); return response()->json(['msg' => 'Bin updated successfully!'], 200); } if ($type == 'hash') { // make sure bin is private, if not, then hash isn't needed if ($record->isPrivate()) { // Bin has private hash, request is to delete if ($record->isShared()) { $record->private_hash = null; $record->save(); return response()->json(['msg' => 'Bin sharing has been disabled!', 'status' => 'disabled'], 200); } else { // No private hash exists, create one $record->private_hash = str_random(30); $record->save(); return response()->json(['msg' => 'Bin sharing enabled!', 'status' => 'enabled', 'url' => $record->shareUrl()], 200); } } return response()->json(['msg' => 'Bin is not private! No need for share url!'], 400); } }
/** * Get the value of the model's route key. * * @return mixed */ public function getRouteKey() { return hashid()->encode($this->getKey()); }
public function url() { return route('bin.snippet', [hashid()->encode($this->bin_id), hashid()->encode($this->id)]); }