在database.php
加上mysql预处理
先贴出代码:
$permissions = [];
$user->roles()->with('perms')->get()->pluck('perms')->map(function ($item, $key) use (&$permissions) {
$child = $item->where('is_display', 1)->pluck('id');
var_dump($child);
if ($key == 0) {
$permissions = $child->toArray();
} else {
$permissions = array_merge($permissions, $child->toArray());
}
});
$item
打印出来是一个eloquent
对象:
object(Illuminate\Database\Eloquent\Collection)[604]
protected 'items' =>
array (size=46)
0 =>
object(App\Models\Permission)[528]
protected 'table' => string 'permissions' (length=11)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=9)
'id' => string '1' (length=1)
'parent_id' => string '0' (length=1)
'name' => string '/' (length=1)
'display_name' => string '管理后台' (length=12)
'description' => string '管理后台' (length=12)
'is_display' => string '1' (length=1)
'icon' => string 'fa-dashboard' (length=12)
'created_at' => string '2017-01-05 16:34:04' (length=19)
'updated_at' => string '2017-01-05 16:34:04' (length=19)
protected 'original' =>
array (size=11)
'id' => string '1' (length=1)
'parent_id' => string '0' (length=1)
'name' => string '/' (length=1)
'display_name' => string '管理后台' (length=12)
'description' => string '管理后台' (length=12)
'is_display' => string '1' (length=1)
'icon' => string 'fa-dashboard' (length=12)
'created_at' => string '2017-01-05 16:34:04' (length=19)
'updated_at' => string '2017-01-05 16:34:04' (length=19)
'pivot_role_id' => string '1' (length=1)
'pivot_permission_id' => string '1' (length=1)
protected 'relations' =>
array (size=1)
'pivot' =>
object(Illuminate\Database\Eloquent\Relations\Pivot)[530]
protected 'parent' =>
object(App\Models\Role)[590]
protected 'fillable' =>
array (size=3)
0 => string 'name' (length=4)
1 => string 'display_name' (length=12)
2 => string 'description' (length=11)
protected 'table' => string 'roles' (length=5)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=0)
empty
protected 'original' =>
array (size=0)
empty
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean false
public 'wasRecentlyCreated' => boolean false
protected 'foreignKey' => string 'role_id' (length=7)
protected 'otherKey' => string 'permission_id' (length=13)
protected 'guarded' =>
array (size=0)
empty
protected 'connection' => null
protected 'table' => string 'permission_role' (length=15)
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean false
protected 'attributes' =>
array (size=2)
'role_id' => string '1' (length=1)
'permission_id' => string '1' (length=1)
protected 'original' =>
array (size=2)
'role_id' => string '1' (length=1)
'permission_id' => string '1' (length=1)
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'fillable' =>
array (size=0)
empty
protected 'dates' =>
array (size=0)
empty
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'fillable' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
................
为什么$child
是empty
的?
$item
对象对应的is_display
字段值为string类型的,导致$child
为空