In this tutorial, we will tell you how to use the redirect route with the query string in laravel 8(Laravel 8 Redirect Route With Query String).
Why use a redirect route with another parameter or query string in laravel application.
sometimes we need to redirect with a success message or error message at that time we will use the redirect route with another parameter or query string in laravel application.
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$students = Student::all();
return view('student.list', compact('students','students'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('student.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate([
'txtFirstName'=>'required',
'txtLastName'=> 'required',
'txtAddress' => 'required'
]);
$student = new Student([
'first_name' => $request->get('txtFirstName'),
'last_name'=> $request->get('txtLastName'),
'address'=> $request->get('txtAddress')
]);
$student->save();
return redirect('/student')->with('success', 'Student has been added');
}
/**
* Display the specified resource.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function show(Student $student)
{
//
return view('student.view',compact('student'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function edit(Student $student)
{
//
return view('student.edit',compact('student'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function update(Request $request,$id)
{
//
$request->validate([
'txtFirstName'=>'required',
'txtLastName'=> 'required',
'txtAddress' => 'required'
]);
$student = Student::find($id);
$student->first_name = $request->get('txtFirstName');
$student->last_name = $request->get('txtLastName');
$student->address = $request->get('txtAddress');
$student->update();
return redirect('/student')->with('success', 'Student updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function destroy(Student $student)
{
//
$student->delete();
return redirect('/student')->with('success', 'Student deleted successfully');
}
}
?>
So here, we have used the redirect route with string parameter in studentController.php. such as store method, update method, and destroy method.