Support the ongoing development of →
Requests Database Eloquent
Last updated 2 years ago.

  //We first define the Form validation rule(s)
  $rules = array(
    'link' => 'required|url'

  //Then we run the form validation
  $validation = Validator::make(Input::all(),$rules);

  //If validation fails, we return to the main page with error info
  if($validation->fails()) {
    return Redirect::to('/')
  } else {
    //Now let's check that if we have the link already in our database, if so we get the first result
    $link = Link::where('url','=',Input::get('link'))
    //If we have the URL saved in our database already, we provide that information back to view.
    if($link) {
      return Redirect::to('/')
    //Else we create a new unique URL
    } else {
      //First we create a new unique Hash
      do {
        $newHash = Str::random(6);
      } while(Link::where('hash','=',$newHash)->count() > 0);

      //Now we create a new database record
        'url' => Input::get('link'),
        'hash'  => $newHash

      //And then we return the new shortened URL info to our action
      return Redirect::to('/')

route code

Last updated 2 years ago.

I suggest using named routes and controllers this code is really unreadable.

Route::get('/', array('as' => 'home', 'uses' => 'Controller@getHome'));
Last updated 2 years ago.

Sign in to participate in this thread!


Your banner here too?

sger sger Joined 15 Feb 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.