How to json response from controller in Laravel ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to json response from controller in Laravel ? If we are working with REST API then we have always need to return json data in response that way the front-end developer can handle it easily.So here i will created the bellow controller method in this method i will return json array response, on that way we can understand very well. We are know that laravel provide us response() helper and it will also help to response of json object. But if we are working on Core PHP then we have to do json_encode before send the output.

Now in laravel we can do it very easily like this way:

Example:

public function getJsonData()
{
    $myArray = ['id'=>1, 'name'=>'HD'];
    return response()->json($myArray);
}

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →