从口才关系中选择特定的列

use*_*908 1 laravel laravel-5

我的应用程序中具有以下模型:

User.php

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Kodeine\Acl\Traits\HasRole;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword, HasRole;

    /**
     * The database table used by the model.
     *
     * @var stringSS
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'is_active'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function customer_details()
    {
        return $this->hasOne('App\Models\CustomerDetails', 'user_id');
    }

}
Run Code Online (Sandbox Code Playgroud)

CustomerDetails.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CustomerDetails extends Model {

    protected $table = 'customer_details';
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
    protected $appends = ['full_name'];

    public function getFullNameAttribute()
    {
        return $this->attributes['first_name'] .' '. $this->attributes['last_name'];
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function invoices() {
        return $this->hasMany('App\Models\Invoices', 'customer_id');
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试运行以下查询:

$user = User::select('email', 'phone')->where('id', Auth::user()->id)->with('customer_details')->first();
Run Code Online (Sandbox Code Playgroud)

现在,我要执行的操作是仅从用户表中选择电子邮件电话号码,并从customer_details表中选择first_namelast_name,但是无论何时尝试,它总是将customer_details返回为null。此功能将仅在一页上使用,这意味着可能还有其他页面可能需要所有详细信息,但并不需要这一页,因此我想创建一个单独的Eloquent关系来执行此操作。

Jos*_*sch 6

您需要id在选择中包括,否则急切的加载没有匹配的对象。我只是针对自己的项目对此进行了测试,并遇到了同样的问题,将其添加id到选择的修复程序中。

$user = User::select('id', 'email', 'phone')->where('id', Auth::user()->id)->with('customer_details')->first();
Run Code Online (Sandbox Code Playgroud)

为了进一步说明这一点,渴望加载的工作方式是先进行您的父级调用(在这种情况下,是对的调用users),然后再进行第二次调用,例如SELECT * FROM customer_details WHERE customer_details.user_id IN (?)where ?是用户ID列表。然后,它将遍历第二次调用的结果,并将其附加到适当的用户对象。如果没有idon用户对象,则在第二个调用中或附加时没有任何匹配的对象。

编辑

要从急切加载的表中选择特定的列,可以使用此处说明的关闭功能。因此,您可以使用关系名称作为键,并使用将Builder对象作为值的闭包,如下所示:

$user = User::select('id', 'email', 'phone')
    ->where('id', Auth::user()->id)
    ->with('customer_details' => function (Builder $query) {
        $query->select('id', 'user_id', 'name', 'foo');
    })
    ->first();
Run Code Online (Sandbox Code Playgroud)

请注意,您必须为两者都选择“关系”列,否则Laravel不知道如何连接两者