gpt4 book ai didi

laravel - toArray() 中缺少延迟加载关系

转载 作者:行者123 更新时间:2023-12-04 01:39:36 28 4
gpt4 key购买 nike

Laravel 5.8

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

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

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

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

知道这里发生了什么吗?

enter image description here

enter image description here

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

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');
}
}

帐户
<?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');
}
}

最佳答案

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

Customer.php :

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

crmaccount不在那个数组中,只有 idname将可用。只需添加 crmaccount到要处理的数组:
protected $visible = ['id','name', 'crmaccount'];

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

关于laravel - toArray() 中缺少延迟加载关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57998244/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com