In this tutorial, we will see how to response use in laravel 8. All Laravel routes and controllers must return a response which can be sent back to user’s browser.
Responses Strings
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return 'rathorji.in';
}
Responses Arrays
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return [1, 2, 3];
}
Responses Objects
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response('rathorji.in', 200)
->header('Content-Type', 'text/plain');
}
Responses Redirects
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect('home/dashboard');
}
Responses Back
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return back()->withInput();
}
Responses Routes
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect()->route('login');
Responses Action
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect()->action([TestController::class, 'index']);
}
Responses Domains
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect()->away('https://www.facebook.com');
}
Responses With
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect('dashboard')->with('status', 'Profile updated!');
}
//display error blade message
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
Responses Json
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->json([
'name' => 'Ravi',
'state' => 'Guj',
]);
}
Responses Download
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->download($pathToFile);
}
Responses File
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->file($pathToFile);
}
I hope this example helps you.