Laravel 8 Response Example Tutorial


Share:
Published By: Admin, Published On: Aug 04, 2021, Category: Laravel 8 Laravel

Hi All,

Today,in this Article,I will share something unique content for you how to response actually works in laravel 8.you can easily understand all resopnse learn in one tutorial.All Laravel routes and controllers must return a response which can be sent back to user’s browser.

Here I will give you all of response how it works in one example tutorial so you can just follow step by step.

Responses Strings

In this first example i will show you response,The mostly basic response is returning a string from a route or controller.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return 'MyWebtuts.com';
}
Responses Arrays

In this example returning a array from a route or controller.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return ['Ramesh','Suresh','Mahesh'];
}
Responses Objects

In this response returning string from a route or controller with status code 200 and passes to the header.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response('MyWebtuts.com', 200)
              ->header('Content-Type', 'text/plain');
}
Responses Redirects

In this controller method,the response redirect to home dashboard route.you can use one URl to rediect another URL user.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return redirect('home');
}
Responses Back

back() response use to user back to after URl.you can multi form write then any query before form at time back response use.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return back()->withInput();
}
Responses Routes

this response is use to specifice route redirect this URL.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return redirect()->route('login');
}
Responses Action

this response is use redirect other controller specifice method call.action call controller in use in your controller.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return redirect()->action([FormController::class, 'index']);
}
Responses Domains

this response is use specifice domain redirect.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return redirect()->away('https://www.facebook.com');
}
Responses With

Responses with is use specifice URl with display message in your bowser.this is done after successfully performing an action when you flash a success message to the session.

/**
* 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

Today,Most of the APIs reflect JSON data. One of the reasons why Laravel is so famous is that it has JSON support right out of the box. That is we don’t need to make any programmatic efforts for this. Json method of Laravel automatically converts the array into JSON form which is utilized in json_encode PHP function.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
	return response()->json([
            'name' => 'Bhavesh',
            'state' => 'Gujarat',
        ]);
}
Responses Download

This response is use to pdf file download in your own desktop.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response()->download($pathToFile);
}
Responses File

This response is use image and file download in your own desktop.

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response()->file($pathToFile);
}

It will help you..

Share: