简体   繁体   English

Eloquent Model 关系不返回子关系特定列值

[英]Eloquent Model relationship does not return sub-relationship specific column value

I have attempted to achieve to pull the user's info and the product details (Which specified the nickname and name) when the product name equal to "Oops" but I have no idea why the getProducts does not return any things.当产品名称等于“哎呀”时,我试图实现提取用户信息和产品详细信息(指定昵称和名称),但我不知道为什么 getProducts 不返回任何东西。

User Model用户 Model

 public function getProducts(){
        return $this->hasMany('App\Models\Product','users_id');
    }

Product Model产品 Model

public function users(){
        return $this->belongsTo(User::class);
    }

The code of pulling data:拉取数据的代码:

$products = User::with(['getProducts' => function($query){
            $query->select("users_id","name","nickname");
        }])->get();

The current output:当前output:

"data": [
    {
      "id": 1,
      "name": "John Smith",
      "email": "john.smith@hotmail.com",
      "email_verified_at": null,
      "created_at": "2021-04-08T13:29:13.000000Z",
      "updated_at": "2021-04-08T13:29:13.000000Z",
      "role": 0,
      "get_products": [
        
      ]
    },
    {
      "id": 2,
      "name": "Kelvin Ooi",
      "email": "kelvin.ooi@hotmail.com",
      "email_verified_at": null,
      "created_at": "2021-04-08T13:29:13.000000Z",
      "updated_at": "2021-04-13T12:07:11.000000Z",
      "role": 1,
      "get_products": [
            {
                "nickname":"MCD",
                "name":"Oops"
            },
            {
                "nickname":"Mary Brown",
                "name":"Oops"
            },
            {
                "nickname":"Kentucy",
                "name":"KFC"
            },
            {
                "nickname":"Texas Chicken",
                "name":"TXS"
            }            
      ]
    }
  ]

The expected output预计output

"data": [
    {
      "id": 1,
      "name": "John Smith",
      "email": "john.smith@hotmail.com",
      "email_verified_at": null,
      "created_at": "2021-04-08T13:29:13.000000Z",
      "updated_at": "2021-04-08T13:29:13.000000Z",
      "role": 0,
      "get_products": [
        
      ]
    },
    {
      "id": 2,
      "name": "Kelvin Ooi",
      "email": "kelvin.ooi@hotmail.com",
      "email_verified_at": null,
      "created_at": "2021-04-08T13:29:13.000000Z",
      "updated_at": "2021-04-13T12:07:11.000000Z",
      "role": 1,
      "get_products": [
            {
                "nickname":"MCD",
                "name":"Oops"
            },
            {
                "nickname":"Mary Brown",
                "name":"Oops"
            }
      ]
    }
  ]

getProducts is not a good name for the relationship, lets simply call it products . getProducts不是关系的好名字,我们简单地称它为products

public function products()
{
    return $this->hasMany('App\Models\Product','users_id');
}

Your code in the comment doesn't work because you specify the where clause in the main query, not the sub query.您在注释中的代码不起作用,因为您在主查询中指定了 where 子句,而不是子查询。

return User::with(['products' => function ($query) { 
    $query->select("users_id","name","nickname"); 
}])
// This got to be in the sub query.
->where("products.name","Oops")
->get();

So let's update your code to this:因此,让我们将您的代码更新为:

$productName = 'Oops';

return User::with(['products' => function ($query) use ($productName) { 
    $query->select("users_id","name","nickname", "price")
        ->where("name","LIKE", "%{$productName}%"); 
}])
->get();

I have seen your comment to this answer.我已经看到您对此答案的评论。 Let's define a total custom attribute for the User model:让我们为User model 定义一个total 自定义属性

class User extends Model
{
    protected $appends = ['total'];

    public function getTotalAttribute()
    {
        // This is higher order message, if you haven't used it: https://laravel.com/docs/8.x/collections#higher-order-messages
        return $this->products->sum->price;
    }
}

Then the total attribute will be part of any user.那么total属性将是任何用户的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM