Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel有许多关系自定义选择结果为空_Php_Laravel_Eloquent_Relationship - Fatal编程技术网

Php Laravel有许多关系自定义选择结果为空

Php Laravel有许多关系自定义选择结果为空,php,laravel,eloquent,relationship,Php,Laravel,Eloquent,Relationship,我有表格、用户、个人资料和联系人。1个用户有1个配置文件,1个配置文件有多个联系人,我想获取带有筛选联系人和选定列的配置文件(我不想获取所有列)。我在下面成功地编写了代码 UserController.php $profile = $user->with(['profile.emails', 'profile.phones'])->first(); public function profile() { return $this->hasOne(Profile::cl

我有表格、用户、个人资料和联系人。1个用户有1个配置文件,1个配置文件有多个联系人,我想获取带有筛选联系人和选定列的配置文件(我不想获取所有列)。我在下面成功地编写了代码

UserController.php

$profile = $user->with(['profile.emails', 'profile.phones'])->first();
public function profile()
{
    return $this->hasOne(Profile::class);
}
public function emails()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'email');
}

public function phones()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'phone');
}
User.php

$profile = $user->with(['profile.emails', 'profile.phones'])->first();
public function profile()
{
    return $this->hasOne(Profile::class);
}
public function emails()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'email');
}

public function phones()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'phone');
}
Profile.php

$profile = $user->with(['profile.emails', 'profile.phones'])->first();
public function profile()
{
    return $this->hasOne(Profile::class);
}
public function emails()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'email');
}

public function phones()
{
    return $this->hasMany(Contact::class)
        ->where('type', 'phone');
}
像这样的结果数据

{
    "username": "user",
    "email": "user@mail.com",
    "profile": {
        "first_name": "Alex",
        "last_name": "Ariana",
        "emails": [
            {
                "id": 1,
                "profile_id": 1,
                "type": "email",
                "prefix": "Personal",
                "value": "alex@mail.com",
                "is_primary": 0,
            },
            {
                "id": 2,
                "profile_id": 1,
                "type": "email",
                "prefix": "Business",
                "value": "business@mail.com",
                "is_primary": 1,
            }
        ],
        "phones": [
            {
                "id": 3,
                "profile_id": 1,
                "type": "phone",
                "prefix": "45",
                "value": "564637345345",
                "is_primary": 1,
            }
        ]
    }
}
    public function emails()
    {
        return $this->hasMany(Contact::class)
            ->select([DB::raw('IFNULL(prefix, "Email") AS type'), 'value AS url', 'is_primary'])
            ->where('type', 'email');
    }

    public function phones()
    {
        return $this->hasMany(Contact::class)
            ->select(['prefix', 'value AS number', 'is_primary'])
            ->where('type', 'phone');
    }
然后我在关系上加上select,如下所示

{
    "username": "user",
    "email": "user@mail.com",
    "profile": {
        "first_name": "Alex",
        "last_name": "Ariana",
        "emails": [
            {
                "id": 1,
                "profile_id": 1,
                "type": "email",
                "prefix": "Personal",
                "value": "alex@mail.com",
                "is_primary": 0,
            },
            {
                "id": 2,
                "profile_id": 1,
                "type": "email",
                "prefix": "Business",
                "value": "business@mail.com",
                "is_primary": 1,
            }
        ],
        "phones": [
            {
                "id": 3,
                "profile_id": 1,
                "type": "phone",
                "prefix": "45",
                "value": "564637345345",
                "is_primary": 1,
            }
        ]
    }
}
    public function emails()
    {
        return $this->hasMany(Contact::class)
            ->select([DB::raw('IFNULL(prefix, "Email") AS type'), 'value AS url', 'is_primary'])
            ->where('type', 'email');
    }

    public function phones()
    {
        return $this->hasMany(Contact::class)
            ->select(['prefix', 'value AS number', 'is_primary'])
            ->where('type', 'phone');
    }
然后我得到了像这样的空联系人数据 {

}


为什么会发生这种情况?如果我通过
hasManyThrough
从用户处访问,它会工作,但当我使用
hasMany
时,结果是空的。

您必须选择关系所需的任何外键,否则它无法将子项与父项匹配,根据关系,您可能需要“id”字段才能匹配记录upok,但为什么在
hasManyThrough
中没有显示问题,不同的只是关系方法外键,您需要将它们链接到其父项的外键在用户的
profile()
关系中是否有自定义选择?如果是,您应该将
id
列添加到selects。我添加了profile\u id,它可以工作,谢谢,但是当我通过用户模型使用hasManyThrough访问时,我只能选择我想要的列(没有profile\u id或id),为什么?