Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public function shorten(Request $request)
  2. {
  3. $url = Validator::make($request->all(), [
  4. 'url' => 'url'
  5. ])->validate()['url'];
  6.  
  7. $url_model = Url::firstOrNew(['url' => $url]);
  8.  
  9. if ($success = $url_model->exists)
  10.  
  11. // the model exists in the database .
  12. // we'll retrieve the shortend url and skip directly to displaying the view .
  13. $shortend_url = $url_model->shortend_url;
  14.  
  15. elseif ($success = $url_model->save()) {
  16.  
  17. // it does not exist, so we'll save it to retrieve the id assigned by the database .
  18. $shortend_url = env('APP_URL') . '/' . $url_model->id;
  19. $success = $url_model->update(compact('shortend_url'));
  20. }
  21.  
  22. return view('result', compact('success', 'url', 'shortend_url'));
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement