Laravel,从JSON中删除空的雄辩的对象属性 [英] Laravel, remove null Eloquent object attributes from JSON

查看:158
本文介绍了Laravel,从JSON中删除空的雄辩的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种优雅的方法从雄辩的对象中删除NULL值?我的对象嵌套有关系。这个特定的调用可能长达1000线,因此我尝试这样做的主要原因是为用户节省带宽,但是服务器性能也是一个考虑因素。

Is there an elegant way to remove NULL values from an Eloquent Object? My object is nested with relationships. This particular call can be 1000s of lines long, so my main reason for trying this is to save bandwidth for the user, but server performance is also a consideration.

我的代码:

$data['locations'] = Location::with('address')->where('user_id', '1')->get();

return Response::json($data);

我尝试了Mutators,但除非我弄错了,否则Mutators对对象键没有控制权,只是值。

I experimented with Mutators, but unless I'm mistaken Mutators don't have power over the object key, just the value.

我也尝试并未能像下面这样使用array_filter:

I also tried and failed to use array_filter like these:

是否有任何PHP函数会删除对象为null的属性?

如何删除空的关联数组条目

编辑根据要求,

{
    "status": "ok",
    "locations": [
        {
            "id": "1",
            "latitude": "12.239107980271",
            "longitude": "109.19479025725",
            "user_time": "",
            "transport": "Bus",
            "title1": "",
            "title2": "",
            "address": {
                "town": "Nha Trang",
                "country": "Vietnam",
                "address": "36-44 Hùng Vương, Lộc Thọ, Nha Trang, Khanh Hoa Province, Vietnam"
            },
            "altitude": {
                "altitude": "10.006237983704"
            },
            "timezone": {
                "offset": "25200"
            },
            "forecast": {
                "icon": "",
                "high_temp": "",
                "low_temp": ""
            }
        },
        {
            "id": "2",

所需响应:

{
    "status": "ok",
    "locations": [
        {
            "id": "1",
            "latitude": "12.239107980271",
            "longitude": "109.19479025725",
            "transport": "Bus",
            "address": {
                "town": "Nha Trang",
                "country": "Vietnam",
                "address": "36-44 Hùng Vương, Lộc Thọ, Nha Trang, Khanh Hoa Province, Vietnam"
            },
            "altitude": {
                "altitude": "10.006237983704"
            },
            "timezone": {
                "offset": "25200"
            }
        },
        {
            "id": "2",

如您所见,我可以简单地遍历整个批次,并删除没有值的任何键-或键的键。我希望Laravel可以提供一种整洁/快速的方法。

As you can see, I could simply loop through the whole lot and remove any keys - or keys of keys - without values. I was hoping Laravel might provide a neat/fast way of doing the same.

我应该补充一点,从技术上讲,只有纬度和经度是必填字段!

I should add that technically only the latitude and longitude are required fields!

推荐答案

3种可能性:


  1. 响应宏,它会清理您的json数据:
    http:// laravel。 com / docs / responses#response-macros

  2. 扩展 Response 并实施您的清理程序在那里。有关详细操作方法,请参见此出色的教程: http://fideloper.com/extend-request- response-laravel

  3. 实施模型中的> jsonSerialize 方法,当您将模型转换为json并将清理例程放在此处时,系统会自动调用该方法。您甚至可以更进一步,为您的 Location 模型编写自己的 Collection 。根据您的数据结构,这可以使事情变得容易一些。可以在这里找到一个很好的教程: http://heera.it/extend- laravel-eloquent-collection-object

  1. Write a response macro which cleans up your json data: http://laravel.com/docs/responses#response-macros
  2. Extend the Response class and implement your cleanup routine there. See this great tutorial for details how to do this: http://fideloper.com/extend-request-response-laravel
  3. Implement the jsonSerialize method in your model which will be automatically called when your model is converted to json and place your cleanup routines there. You can even go a step further and write your own Collection for your Location model. Depending on your data structure this can make things a little bit easier. A nice tutorial for this purpose can be found here: http://heera.it/extend-laravel-eloquent-collection-object

我个人更喜欢选项3。)因为数据修改发生在应该修改的地方发生-在您的模型中。

I personally would prefer option 3.) because the data modifications happens where it should happen - in your model.

但最重要的是,这实际上取决于哪种解决方案最适合您的项目。

But bottom line it really depends which solutions fits best to your project.

这篇关于Laravel,从JSON中删除空的雄辩的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆