40

I am trying to return Response::json('data', $request); however, I am getting an error:

FatalErrorException in ProjectsController.php line 74: Call to undefined method Illuminate\Http\Response::json()

Where is the Response::json() is located? What am I doing wrong?

1
  • You can use also the response() Helper. return response(['errorMsg' => [...]], 400); If your response is an Array then the helper will convert the array to json and set the right to header application/json. Commented Jun 30, 2021 at 8:20

6 Answers 6

95

use the helper function in laravel 5.1 instead:

return response()->json(['name' => 'Abigail', 'state' => 'CA']);

This will create an instance of \Illuminate\Routing\ResponseFactory. See the phpDocs for possible parameters below:

/**
* Return a new JSON response from the application.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Symfony\Component\HttpFoundation\Response 
* @static 
*/
public static function json($data = array(), $status = 200, $headers = array(), $options = 0){

    return \Illuminate\Routing\ResponseFactory::json($data, $status, $headers, $options);
}
Sign up to request clarification or add additional context in comments.

6 Comments

offtopic, how to get these data in view? Just using $name for 'Abigail' and $state for 'CA' ?
depends on your usage. a xhr request (ajax) would hold an object with name and state property @MASh
Can you explain a lil bit more? I am beginner.
I'm not sure what I can explain.. Maybe you should ask a new question and include details on what you want to achieve... @MASh
if I use view(someview)->with('name', "alex"); then in view I can get using {{ $name }}. Like this will I get 'CA' using {{ $state }}?
|
12

You need to add use Response; facade in header at your file.

Only then you can successfully retrieve your data with

return Response::json($data);

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".
11

After enough googling I found the answer from controller you need only a backslash like return \Response::json(['success' => 'hi, atiq']); . Or you can just return the array return array('success' => 'hi, atiq'); which will be rendered as json in Laravel version 5.2 .

1 Comment

Yes, You're a life saver, but why we didn't use "use" statement. You should have explained the why part as well!
4

However, the previous answer could still be confusing for some programmers. Most especially beginners who are most probably using an older book or tutorial. Or perhaps you still feel the facade is needed. Sure you can use it. Me for one I still love to use the facade, this is because some times while building my api I forget to use the '\' before the Response.

if you are like me, simply add

   "use Response;"

above your class ...extends contoller. this should do.

with this you can now use:

$response = Response::json($posts, 200);

instead of:

$response = \Response::json($posts, 200);

Comments

1

From a controller you can also return an Object/Array and it will be sent as a JSON response (including the correct HTTP headers).

public function show($id)
{
    return Customer::find($id);
}

Comments

1

although Response::json() is not getting popular of recent, that does not stop you and Me from using it. In fact you don't need any facade to use it,

instead of:

$response = Response::json($messages, 200);

Use this:

$response = \Response::json($messages, 200);

with the slash, you are sure good to go.

1 Comment

you mean you don't need an "alias" to use it? because that is the Facade you are referencing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.