懒惰的渴望加载关系在toArray()中丢失

时间:2019-09-18 17:44:55

标签: laravel laravel-5 eloquent

Laravel 5.8

我很懒惰地向用户加载与crmaccount对象具有一对一关系的相关客户

模型正在运行,因此当我检索到渴望加载的实体时,它将显示所有嵌套关系。

稍后,我在该对象上使用“ toArray()”方法,并且输出缺少第三级关系。

关于“ crmaccount”模型的唯一可能特殊之处在于,它包含一个必须是json和必须转换的列。

有什么想法吗?

enter image description here

enter image description here

所有这些都发生在中间件中。如果我使用或加载没有区别。

public function handle($request, Closure $next)
{
    $UserData = \Auth::user();
    if($UserData){
        $User = \App\Login::with(['role','customer','customer.crmaccount'])->find($UserData->id);
        dump($User);
        dd($User->toArray());


        $UserData['isAdmin'] = false;
        if($UserData['role']['name'] === 'Admin'){
            $UserData['isAdmin'] = true;
        }
        $request->request->add(['UserData' => $UserData]);
    }

    return $next($request);
}

登录

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Login extends Authenticatable{
    use Notifiable;

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /* */
    public function Role(){
        return $this->belongsTo('App\Role');
    }
    public function Customer(){
        return $this->belongsTo('App\Customer');
    }
    /**
     * [hasOpportunities Ruft alle Opportunities des Users ab. Da diese lediglich zwei Entitäten weiter sind, kann anstatt von dot-notated Lazy-Load auch die hasManyThrough-ORM-Methode genutzt werden]
     * @return [hasManyThrough-Relation] [Die hasManyThrough-ORM-Beziehung]
     */
    public function hasOpportunities(){
        return $this->hasManyThrough(
            'App\Opportunity',
            'App\Customer',

            'id',
            'customer_id',
            'customer_id'
        );
    }
    /**
     * [hasSalesreps Ruft alle SalesReps des Users ab. Da diese lediglich zwei Entitäten weiter sind, kann anstatt von dot-notated Lazy-Load auch die hasManyThrough-ORM-Methode genutzt werden]
     * @return [hasManyThrough-Relation] [Die hasManyThrough-ORM-Beziehung]
     */
    public function hasSalesreps(){
        return $this->hasManyThrough(
            'App\Salesrep',
            'App\Customer',

            'id',
            'customer_id',
            'customer_id'
        );
    }
}

客户

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model{
    public $timestamps = false;

    protected $visible = ['id','name'];

    protected $fillable = ['name'];


    public function crmaccount(){
        return $this->hasOne('App\Crmaccount');
    }

    public function Salesreps()
    {
        return $this->hasMany('App\Salesrep');
    }

    public function Prospects()
    {
        return $this->hasMany('App\Prospect');
    }

    public function Trees()
    {
        return $this->hasMany('App\Salesreptrees');
    }

    public function Opportunities()
    {
        return $this->hasMany('App\Opportunity');
    }

    public function User()
    {
        return $this->hasMany('App\Login');
    }
}

Crmaccount

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Crmaccount extends Model{
    public $timestamps = false;
    protected $visible = ['id','name','crm_system','customer_id','crm_api_config'];
    protected $fillable = [
        'name','crm_system','customer_id','crm_api_config'
    ];
    protected $casts = [
        'crm_api_config' => 'array'
    ];
    public function customer(){
        return $this->belongsTo('App\Customer');
    }
}

1 个答案:

答案 0 :(得分:3)

在每个模型上,都有一个protected $visible = [];protected $hidden = []属性。这些属性控制将模型转换为objectarrayjson时可用的属性。这包括relationships,因为Laravel在内部将它们转换为属性,因此从visible中省略它们,或将它们包括在hidden中将导致它们不可用。

Customer.php中:

protected $visible = ['id','name'];

由于crmaccount不在该数组中,因此只有idname可用。只需将crmaccount添加到数组即可处理:

protected $visible = ['id','name', 'crmaccount'];

或者,使用hidden显式设置您不想显示的属性,如果relationship通过->with()加载,则默认显示。