How to redirect back in Laravel with Form Input and many possible ways
Categories - Laravel PHP Framework PHP Tags - PHP Laravel   Maniruzzaman Akash   1 year ago   4617   0

How to redirect back in Laravel with Form Input and many possible ways

Laravel enters with so many ways to redirect back by url, route, form data and so on. Today, we'll dive into this.

 

Basic ways to redirection in laravel

Using URL: We want to redirect with a page with url.

return redirect('/url');

Redirection in laravel using URL is simple. Just pass the url inside the redirect() function as params.

 

Using Route: We want to redirect with a page with a route name.

return redirect()->route('post.index');

Redirection in laravel using route is pretty similar. After the redirect() function just add specific route() to redirect. In the above example, We've added post.index route.

 

Basic ways to redirection Back Page in laravel

Simple Use: We want to just back to the previous page. 

return back();

Just simple back() function will do the job to redirect to back page where we were before that page. It is helpful in a case like, after do something in controller, put a session flash message and return back to previous page. Like the below example.

public function store ( Request $request ) {

   // Validation Logic

   // Store Logic

   if( successStored ) {
     session()->flash( 'success', 'Stored Successfully !' );
     return redirect()->route( 'posts.index' ); // Redirect to Posts List Page
   }

   session()->flash( 'error', 'Something wrong happend !' );
   return back(); // Redirect to Back Page
}

This is a simple real-life store() function, which will store posts to database. What tasks it will do in common inside a store function - 

  1. Validate Input Logic
  2. Store Data
  3. If true
    1. Keep session flash success message and
    2. redirect to posts list page
  4. else
    1. Kepp session flash error message and
    2. redirect to back page

 NB: This back() use session and that means, you should keep that in web middleware definitely. 

 

Redirect with form data: We want to redirect to back page with input form data.

return back()->withInput();

It will return to back page with the form input. By default this will back all of the previous page request data to html page. That means  the above code and following code is same.

return back()->withInput();

return back()->withInput(Input::all());

 

And in html page, we would use that something like this -  

<input name="age" type="number" value="{{ old('age') }}" />

This is super simple to get previous page data, right?

 

Now, if you want to get only specific field data from request, you can do that by simple tricks - 

return back()->withInput($request->only('age', 'name'));

 Here, we got only age, name in previous back page.

 

So, get a full example of show back page data with form and actually it's a real one.

// In Controller
public function store ( Request $request ) {

   // Validation Logic

   // Store Logic

   if( successStored ) {
     session()->flash( 'success', 'Stored Successfully !' );
     return redirect()->route( 'posts.index' ); // Redirect to Posts List Page
   }

   session()->flash( 'error', 'Something wrong happend !' );
   return back()->withInput(); // Redirect to Back Page with input
}


// In view page HTML - Create Page

<form method="post" action="{{ route('post.store') }}">
  @csrf

  <input type="text" name="title" value="{{ old('title') }}" class="input"/>

  <input type="number" name="priority" value="{{ old('priority') }}" class="input"/>

  <input type="submit" value="Submit" />
</form>


// In view page HTML - Edit Page

  <input type="text" name="title" value="{{ $post->title ? $post->title : old('title') }}" class="input"/>

  <input type="number" name="priority" value="{{ $post->priority ? $post->priority : old('priority') }}" class="input"/>

 

Hence any confusion, just sign in and comment. Answers in stackoverflow too - https://stackoverflow.com/a/46577713/5543577

Previous
PHP If-else-elseif and Switch-case
Next
PHP String Functions - All necessary String functions in PHP to manage strings better.