Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Web Routes
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here is where you can register web routes for your application. These
  9. | routes are loaded by the RouteServiceProvider within a group which
  10. | contains the "web" middleware group. Now create something great!
  11. |
  12. */
  13.  
  14. Route::get('/', 'LinkController@create');
  15.  
  16. Route::post('/create', 'LinkController@store');
  17.  
  18. Route::post('show/{id}', 'LinkController@show')->name('show');
  19.  
  20. public function store(Request $request)
  21. {
  22. $this->validate($request, [
  23. 'url' => 'required|url'
  24. ]);
  25.  
  26. // Generate string length of 6 characters
  27. $newHash = Str::random(6);
  28.  
  29. // creates a $link object
  30. $link = new Link;
  31.  
  32. //checks if link already exists in the database
  33. $link_in_db = DB::table('links')->where('url', '=', $request->url)->get();
  34.  
  35. if($link_in_db === null){
  36. // sets the $link variables
  37. $link->url = $request->url;
  38. $link->hash = $newHash;
  39.  
  40. // $link is saved in the database
  41. $link->save();
  42.  
  43. // redirects to the route
  44. return redirect()->route('show', $link->id);
  45. }else{ // link is in the database
  46. // print_r($link_in_db); // testing purposes
  47. return redirect()->route('show', $link->id);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement