Вы должны добавить заголовок Accept
, установленный в application/json
, к вашему запросу почтальона на вкладке заголовков следующим образом:
![Postman screenshot](https://i.stack.imgur.com/aNTSL.png)
Этоскажет Laravel, что вы хотите получить ответ json вместо HTML.То же самое относится к любому запросу внутри вашего приложения.
Как вы можете видеть, это проверяется на объекте Illuminate\Http\Response
, когда полезная нагрузка установлена, она проверяет, должен ли он быть преобразован в JSON:
/**
* Set the content on the response.
*
* @param mixed $content
* @return $this
*/
public function setContent($content)
{
$this->original = $content;
// If the content is "JSONable" we will set the appropriate header and convert
// the content to JSON. This is useful when returning something like models
// from routes that will be automatically transformed to their JSON form.
if ($this->shouldBeJson($content)) {
$this->header('Content-Type', 'application/json');
$content = $this->morphToJson($content);
}
// If this content implements the "Renderable" interface then we will call the
// render method on the object so we will avoid any "__toString" exceptions
// that might be thrown and have their errors obscured by PHP's handling.
elseif ($content instanceof Renderable) {
$content = $content->render();
}
parent::setContent($content);
return $this;
}
Вы можете найти код здесь .
Надеюсь, это поможет вам.