toArray() 中缺少惰性急切加载关系

C4p*_*bis 4 laravel eloquent laravel-5

拉拉维尔 5.8

\n\n

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

\n\n

这些模型正在工作,因此当我检索急切加载的实体时,它会显示所有嵌套关系。

\n\n

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

\n\n

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

\n\n

知道这是怎么回事吗?

\n\n

在此输入图像描述

\n\n

在此输入图像描述

\n\n

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

\n\n
public function handle($request, Closure $next)\n{\n    $UserData = \\Auth::user();\n    if($UserData){\n        $User = \\App\\Login::with([\'role\',\'customer\',\'customer.crmaccount\'])->find($UserData->id);\n        dump($User);\n        dd($User->toArray());\n\n\n        $UserData[\'isAdmin\'] = false;\n        if($UserData[\'role\'][\'name\'] === \'Admin\'){\n            $UserData[\'isAdmin\'] = true;\n        }\n        $request->request->add([\'UserData\' => $UserData]);\n    }\n\n    return $next($request);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

登录

\n\n
<?php\n\nnamespace App;\n\nuse Illuminate\\Notifications\\Notifiable;\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\n\nclass Login extends Authenticatable{\n    use Notifiable;\n\n    /**\n     * The attributes that are mass assignable.\n     *\n     * @var array\n     */\n    protected $fillable = [\n        \'name\', \'email\', \'password\',\'customer_id\',\'role_id\'\n    ];\n\n    /**\n     * The attributes that should be hidden for arrays.\n     *\n     * @var array\n     */\n    protected $hidden = [\n        \'password\', \'remember_token\',\n    ];\n\n    /**\n     * The attributes that should be cast to native types.\n     *\n     * @var array\n     */\n    protected $casts = [\n        \'email_verified_at\' => \'datetime\',\n    ];\n    /* */\n    public function Role(){\n        return $this->belongsTo(\'App\\Role\');\n    }\n    public function Customer(){\n        return $this->belongsTo(\'App\\Customer\');\n    }\n    /**\n     * [hasOpportunities Ruft alle Opportunities des Users ab. Da diese lediglich zwei Entit\xc3\xa4ten weiter sind, kann anstatt von dot-notated Lazy-Load auch die hasManyThrough-ORM-Methode genutzt werden]\n     * @return [hasManyThrough-Relation] [Die hasManyThrough-ORM-Beziehung]\n     */\n    public function hasOpportunities(){\n        return $this->hasManyThrough(\n            \'App\\Opportunity\',\n            \'App\\Customer\',\n\n            \'id\',\n            \'customer_id\',\n            \'customer_id\'\n        );\n    }\n    /**\n     * [hasSalesreps Ruft alle SalesReps des Users ab. Da diese lediglich zwei Entit\xc3\xa4ten weiter sind, kann anstatt von dot-notated Lazy-Load auch die hasManyThrough-ORM-Methode genutzt werden]\n     * @return [hasManyThrough-Relation] [Die hasManyThrough-ORM-Beziehung]\n     */\n    public function hasSalesreps(){\n        return $this->hasManyThrough(\n            \'App\\Salesrep\',\n            \'App\\Customer\',\n\n            \'id\',\n            \'customer_id\',\n            \'customer_id\'\n        );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

顾客

\n\n
<?php\n\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Customer extends Model{\n    public $timestamps = false;\n\n    protected $visible = [\'id\',\'name\'];\n\n    protected $fillable = [\'name\'];\n\n\n    public function crmaccount(){\n        return $this->hasOne(\'App\\Crmaccount\');\n    }\n\n    public function Salesreps()\n    {\n        return $this->hasMany(\'App\\Salesrep\');\n    }\n\n    public function Prospects()\n    {\n        return $this->hasMany(\'App\\Prospect\');\n    }\n\n    public function Trees()\n    {\n        return $this->hasMany(\'App\\Salesreptrees\');\n    }\n\n    public function Opportunities()\n    {\n        return $this->hasMany(\'App\\Opportunity\');\n    }\n\n    public function User()\n    {\n        return $this->hasMany(\'App\\Login\');\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

crmaccount

\n\n
<?php\n\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Crmaccount extends Model{\n    public $timestamps = false;\n    protected $visible = [\'id\',\'name\',\'crm_system\',\'customer_id\',\'crm_api_config\'];\n    protected $fillable = [\n        \'name\',\'crm_system\',\'customer_id\',\'crm_api_config\'\n    ];\n    protected $casts = [\n        \'crm_api_config\' => \'array\'\n    ];\n    public function customer(){\n        return $this->belongsTo(\'App\\Customer\');\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Tim*_*wis 5

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

Customer.php

protected $visible = ['id','name'];
Run Code Online (Sandbox Code Playgroud)

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

protected $visible = ['id','name', 'crmaccount'];
Run Code Online (Sandbox Code Playgroud)

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