laravel 集合中的某些对象无法与用户模型关联
some objects in a laravel collection can not relation with user model
我在 laravel 中获得了我的项目数据...
$aProjects = Project::all();
我想获得项目创建者的全名...
foreach ($aProjects as $key => $project) {
$sFullName = $project->user->getFullName();
$project->fullName = $sFullName;
}
尽管所有项目数据都相同,但我只获得了第一个 $project fullName
。
对于其他人,我有 null
这是用户模型
中的getFullName()
函数
public function getFullName()
{
return $this->firstName ." ".$this->lastName;
}
whit 这个 foreach
$a = [] ;
foreach ($aProjects as $key => $project) {
$a[$key]= $project->user;
}
dd($a);
laravel return
array:9 [
0 => App\User {#330
#fillable: array:5 [
0 => "firstName"
1 => "lastName"
2 => "userName"
3 => "email"
4 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#casts: array:1 [
"users_email_verified_at" => "datetime"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [
"id" => 1
"firstName" => "komail"
"lastName" => "fayazbakhsh"
"role" => 1
"email" => "komail.f67@gmail.com"
"email_verified_at" => null
"password" => "y$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
"remember_token" => null
"created_at" => "2019-12-30 19:41:11"
"updated_at" => "2019-12-30 19:41:11"
]
#original: array:10 [
"id" => 1
"firstName" => "komail"
"lastName" => "fayazbakhsh"
"role" => 1
"email" => "komail.f67@gmail.com"
"email_verified_at" => null
"password" => "y$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
"remember_token" => null
"created_at" => "2019-12-30 19:41:11"
"updated_at" => "2019-12-30 19:41:11"
]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
1 => App\User {#339
#fillable: array:5 [
0 => "firstName"
1 => "lastName"
2 => "userName"
3 => "email"
4 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#casts: array:1 [
"users_email_verified_at" => "datetime"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [
"id" => 2
"firstName" => "kasra"
"lastName" => "karami"
"role" => 3
"email" => "kasra@gmail.com"
"email_verified_at" => null
"password" => "y$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
"remember_token" => null
"created_at" => "2019-12-30 19:42:20"
"updated_at" => "2019-12-30 19:42:20"
]
#original: array:10 [
"id" => 2
"firstName" => "kasra"
"lastName" => "karami"
"role" => 3
"email" => "kasra@gmail.com"
"email_verified_at" => null
"password" => "y$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
"remember_token" => null
"created_at" => "2019-12-30 19:42:20"
"updated_at" => "2019-12-30 19:42:20"
]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
2 => null
3 => null
4 => null
5 => null
6 => null
7 => null
8 => null
]
这些空值的原因是什么?
您可以使用Laravel Accessor代替这种方式。
在Project
模型中定义一个访问器,像这样:
public function getFullNameAttribute()
{
return $this->user->firstName + " " + $this->user->lastName;
}
您可以像这样使用此属性:
$project->full_name;
如果您希望将这些计算值添加到模型的数组/JSON 表示中,您应该在 Project
模型中使用 appends,例如:
protected $appends = ["full_name"]
public function getFullNameAttribute()
{
return $this->user->firstName + " " + $this->user->lastName;
}
我在 laravel 中获得了我的项目数据...
$aProjects = Project::all();
我想获得项目创建者的全名...
foreach ($aProjects as $key => $project) {
$sFullName = $project->user->getFullName();
$project->fullName = $sFullName;
}
尽管所有项目数据都相同,但我只获得了第一个 $project fullName
。
对于其他人,我有 null
这是用户模型
中的getFullName()
函数
public function getFullName()
{
return $this->firstName ." ".$this->lastName;
}
whit 这个 foreach
$a = [] ;
foreach ($aProjects as $key => $project) {
$a[$key]= $project->user;
}
dd($a);
laravel return
array:9 [
0 => App\User {#330
#fillable: array:5 [
0 => "firstName"
1 => "lastName"
2 => "userName"
3 => "email"
4 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#casts: array:1 [
"users_email_verified_at" => "datetime"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [
"id" => 1
"firstName" => "komail"
"lastName" => "fayazbakhsh"
"role" => 1
"email" => "komail.f67@gmail.com"
"email_verified_at" => null
"password" => "y$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
"remember_token" => null
"created_at" => "2019-12-30 19:41:11"
"updated_at" => "2019-12-30 19:41:11"
]
#original: array:10 [
"id" => 1
"firstName" => "komail"
"lastName" => "fayazbakhsh"
"role" => 1
"email" => "komail.f67@gmail.com"
"email_verified_at" => null
"password" => "y$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
"remember_token" => null
"created_at" => "2019-12-30 19:41:11"
"updated_at" => "2019-12-30 19:41:11"
]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
1 => App\User {#339
#fillable: array:5 [
0 => "firstName"
1 => "lastName"
2 => "userName"
3 => "email"
4 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#casts: array:1 [
"users_email_verified_at" => "datetime"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [
"id" => 2
"firstName" => "kasra"
"lastName" => "karami"
"role" => 3
"email" => "kasra@gmail.com"
"email_verified_at" => null
"password" => "y$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
"remember_token" => null
"created_at" => "2019-12-30 19:42:20"
"updated_at" => "2019-12-30 19:42:20"
]
#original: array:10 [
"id" => 2
"firstName" => "kasra"
"lastName" => "karami"
"role" => 3
"email" => "kasra@gmail.com"
"email_verified_at" => null
"password" => "y$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
"remember_token" => null
"created_at" => "2019-12-30 19:42:20"
"updated_at" => "2019-12-30 19:42:20"
]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
2 => null
3 => null
4 => null
5 => null
6 => null
7 => null
8 => null
]
这些空值的原因是什么?
您可以使用Laravel Accessor代替这种方式。
在Project
模型中定义一个访问器,像这样:
public function getFullNameAttribute()
{
return $this->user->firstName + " " + $this->user->lastName;
}
您可以像这样使用此属性:
$project->full_name;
如果您希望将这些计算值添加到模型的数组/JSON 表示中,您应该在 Project
模型中使用 appends,例如:
protected $appends = ["full_name"]
public function getFullNameAttribute()
{
return $this->user->firstName + " " + $this->user->lastName;
}