0

Is it possible to show return json data as the API response and use the same data for a view?

1 Answer 1

2

Yes.

$data = ["a_variable" => "its_value"];

if (request()->wantsJson()) {
    return response()->json($data);
}

return view('someview', $data);

Assume the call returns $data with the response.

Sign up to request clarification or add additional context in comments.

1 Comment

This is working. How did you test and what's the error you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.