如何使用预加载从 eloquent 关系中仅作为 json 响应对象中的字符串列返回

How to returning only as string column in json response object from eloquent relationship using eager loading

我正在尝试弄清楚如何从相关 table.

中以字符串而不是数组对象的形式急切加载数据

我有 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_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 是否有可能和任何聪明的方法?

简单:)

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

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;