【问题标题】:How to returning only as string column in json response object from eloquent relationship using eager loading如何使用急切加载从雄辩的关系中仅返回json响应对象中的字符串列
【发布时间】:2021-07-27 16:03:02
【问题描述】:

我试图弄清楚如何将数据作为字符串而不是相关表中的数组对象预先加载。

我有 3 个模型,这里是关系

Product.php

class Product extends Model
    {
         public function ProductTag()
        {
            return $this->hasMany(ProductTag::class);
        }
        public function Category()
        {
            return $this->belongsTo(Category::class);
        }
    }

ProductTag.php

class ProductTag extends Model
    {
        public function Product()
        {
            return $this->belongsTo(Product::class);
        }
    }

Category.php

class Category extends Model
    {
        public function Product()
        {
            return $this->hasMany(Product::class);
        }
    }

我试过这样做:

public function tag(){
        return $this->hasMany(ProductTag::class)
            ->selectRaw('GROUP_CONCAT(tag) as tag,id')
            ->groupBy('id');
        }


public static function list(){
    $result = Category::with(['Product'=>function($q){
        $q->with(['tag'=>function($q1){
            $q1->first();
        }]);
   }])->get();
}

这是回复:

{
    "data": {
        "categories": [
            {
                "id": 1,
                "category": "test 1",
                "product": [
                    {
                        "id": 46,
                        "name": "test prod 1",
                        "tag": []
                    },
                    {...}
                ]
            },
            {
                "id": 2,
                "category": "test 2",
                "product": [
                    {
                        "id": 45,
                        "name": "test prod 2",
                        "tag": [
                            {
                                "product_tag": "Test1, test12, test123"
                            }
                        ]
                    },
                    {...}
                ]
            },
            {
                "id": 3,
                "category": "test 3",
                "product": []
            }
        ]
    }
}

除了标签数组之外,响应与预期的一样,因此,我可以在 "product" 数组中获得 "product_tag" 而不是名为“tag”的数组

"product": [
                    {
                        "id": 45,
                        "name": "test prod 2",
                        "tag": [
                            {
                                "product_tag": "Test1, test12, test123"
                            }
                        ]
                    }
            ]
            

这是我想要的样子:

"product": [
                    {
                        "id": 45,
                        "name": "test prod 2",
                        "product_tag": "Test1, test12, test123"
                    }
            ]

在 Laravel 中使用 Eloquent 是否有可能以及任何聪明的方式来做到这一点?

【问题讨论】:

    标签: laravel-8 eloquent-relationship jsonresponse


    【解决方案1】:

    简单:)

    顺便说一句,如果可以的话 - 重命名 product_tag 以响应 tag_line 或相同 - 为关系和 mutator 取相同的名称不是正确的方法。

    class Product extends Model
        {
             public function getTagLineAttribute()
             {
                 //tag - is "name" field in ProductTag
                 return $this->ProductTag->pluck('tag')->implode(',');
             }
             public function ProductTag()
            {
                return $this->hasMany(ProductTag::class);
            }
            public function Category()
            {
                return $this->belongsTo(Category::class);
            }
        }
    
    //eager loading with tags
    Product::with('ProductTag')->find(..)->tagLine;
    

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 2015-09-17
      • 2019-09-14
      • 2021-02-03
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多