Laravel 5.4 高阶消息传递
Laravel 5.4 Higher Order Messaging
问题:为什么旧方法 returns 正确的值看到结果旧方法和新 HOM 方法 returns 整个集合?
榜样
class Role extends Model {
public function getName() {
return $this->name;
}
}
控制器
$roles = Role::all(); // get all roles to test
// old approach
$roles->each(function(Role $i) {
var_dump($i->getName());
});
// new approach (HOM)
var_dump($roles->each->getName());
如果我使用更高阶的消息传递实现新方法 returns 整个集合,如果我使用旧方法,我会得到正确的结果。
结果旧方法
string(11) "Application"
string(6) "System"
string(7) "Network"
string(7) "Manager"
结果新方法
object(Illuminate\Database\Eloquent\Collection)#196 (1) {
["items":protected]=>
array(4) {
[0]=>
object(App\Modules\Role\Role)#197 (24) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(11) "Application"
["description"]=>
string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat."
["created_at"]=>
string(19) "2017-03-03 11:56:09"
["updated_at"]=>
string(19) "2017-03-03 11:56:09"
}
["original":protected]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(11) "Application"
["description"]=>
string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat."
["created_at"]=>
string(19) "2017-03-03 11:56:09"
["updated_at"]=>
string(19) "2017-03-03 11:56:09"
}
["casts":protected]=>
array(0) {
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["appends":protected]=>
array(0) {
}
["events":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["timestamps"]=>
bool(true)
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
}
}
each
只是遍历集合,它实际上并没有 return 任何东西。如果您希望它在每次迭代中 return getName()
的值,那么您可以使用 map 例如
dump($roles->map->getName());
希望对您有所帮助!
问题:为什么旧方法 returns 正确的值看到结果旧方法和新 HOM 方法 returns 整个集合?
榜样
class Role extends Model {
public function getName() {
return $this->name;
}
}
控制器
$roles = Role::all(); // get all roles to test
// old approach
$roles->each(function(Role $i) {
var_dump($i->getName());
});
// new approach (HOM)
var_dump($roles->each->getName());
如果我使用更高阶的消息传递实现新方法 returns 整个集合,如果我使用旧方法,我会得到正确的结果。
结果旧方法
string(11) "Application"
string(6) "System"
string(7) "Network"
string(7) "Manager"
结果新方法
object(Illuminate\Database\Eloquent\Collection)#196 (1) {
["items":protected]=>
array(4) {
[0]=>
object(App\Modules\Role\Role)#197 (24) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(11) "Application"
["description"]=>
string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat."
["created_at"]=>
string(19) "2017-03-03 11:56:09"
["updated_at"]=>
string(19) "2017-03-03 11:56:09"
}
["original":protected]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(11) "Application"
["description"]=>
string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat."
["created_at"]=>
string(19) "2017-03-03 11:56:09"
["updated_at"]=>
string(19) "2017-03-03 11:56:09"
}
["casts":protected]=>
array(0) {
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["appends":protected]=>
array(0) {
}
["events":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["timestamps"]=>
bool(true)
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
}
}
each
只是遍历集合,它实际上并没有 return 任何东西。如果您希望它在每次迭代中 return getName()
的值,那么您可以使用 map 例如
dump($roles->map->getName());
希望对您有所帮助!