Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- |--------------------------------------------------------------------------
- | Web Routes
- |--------------------------------------------------------------------------
- |
- | Here is where you can register web routes for your application. These
- | routes are loaded by the RouteServiceProvider within a group which
- | contains the "web" middleware group. Now create something great!
- |
- */
- Route::get('/', 'LinkController@create');
- Route::post('/create', 'LinkController@store');
- Route::post('show/{id}', 'LinkController@show')->name('show');
- public function store(Request $request)
- {
- $this->validate($request, [
- 'url' => 'required|url'
- ]);
- // Generate string length of 6 characters
- $newHash = Str::random(6);
- // creates a $link object
- $link = new Link;
- //checks if link already exists in the database
- $link_in_db = DB::table('links')->where('url', '=', $request->url)->get();
- if($link_in_db === null){
- // sets the $link variables
- $link->url = $request->url;
- $link->hash = $newHash;
- // $link is saved in the database
- $link->save();
- // redirects to the route
- return redirect()->route('show', $link->id);
- }else{ // link is in the database
- // print_r($link_in_db); // testing purposes
- return redirect()->route('show', $link->id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement