Returning an Eloquent model as JSON in Laravel 4

前端 未结 4 784
既然无缘
既然无缘 2021-01-01 09:13

How do you return an Eloquent model to the browser as JSON? What is the difference between the two methods below? Both seems to work.

#1:

         


        
4条回答
  •  悲&欢浪女
    2021-01-01 09:44

    Response::json($someArray) is a generic way to return JSON data.

    return $model->toJson() is specific to returning a model as JSON. This would be my preferred approach when working with an Eloquent model.

提交回复
热议问题