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 -
- Validate Input Logic
- Store Data
- If true
- Keep session flash
success
message and - redirect to posts list page
- Keep session flash
- else
- Kepp session flash
error
message and - redirect to back page
- Kepp session flash
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
PHP If-else-elseif and Switch-case
PHP String Functions - All necessary String functions in PHP to manage strings better.
Popular Tutorials
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development