【发布时间】:2017-04-15 00:51:29
【问题描述】:
我的数据库输出是这样的:
[{"id":1,"domain":"test","email":"test@mail.com","status":"pending"},{"id":3,"domain":"tester","email":"lorem@ipsum.ml","status":"pending"}]
如果转换为 JSON:
[
{
"id": 1,
"domain": "test",
"email": "test@mail.com",
"status": "pending"
},
{
"id": 3,
"domain": "tester",
"email": "lorem@ipsum.ml",
"status": "pending"
}
]
我想像这样按字母(所有字母)对它们进行分组:
[
"a" :{
}
"b" :{
}
"l" :{
"id": 3,
"domain": "lorem",
"email": "lorem@ipsum.ml",
"status": "pending"
}
"t":{
"id": 1,
"domain": "test",
"email": "test@mail.com",
"status": "pending"
},
]
这样我就可以通过使用{{ $json[$char'] or 'No directories in ' . $char}} 轻松地在视图中显示它们。 $char 来自循环@foreach (range('A', 'Z') as $char)
这是来自我的控制器:
public function all()
{
$data = Subdomain::all();
return view('directories.all' , ['data' => $data]);
}
我的观点是:
@foreach (range('A', 'Z') as $char)
<div class="col-md-3">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">{{$char}}</h3>
</div>
<div class="panel-body">
</div>
<div class="panel-footer">
</div>
</div>
</div>
@endforeach
这是 var_dump:
object(Illuminate\Database\Eloquent\Collection)#188 (1) { ["items":protected]=> array(3) { [0]=> object(App\Subdomain)#189 (23) { ["timestamps"]=> bool(false) ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["attributes":protected]=> array(5) { ["id"]=> int(1) ["domain"]=> string(4) "test" ["email"]=> string(13) "test@mail.com" ["password"]=> string(4) "test" ["status"]=> string(7) "pending" } ["original":protected]=> array(5) { ["id"]=> int(1) ["domain"]=> string(4) "test" ["email"]=> string(13) "test@mail.com" ["password"]=> string(4) "test" ["status"]=> string(7) "pending" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) } [1]=> object(App\Subdomain)#190 (23) { ["timestamps"]=> bool(false) ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["attributes":protected]=> array(5) { ["id"]=> int(3) ["domain"]=> string(6) "tester" ["email"]=> string(14) "lorem@ipsum.ml" ["password"]=> string(0) "" ["status"]=> string(7) "pending" } ["original":protected]=> array(5) { ["id"]=> int(3) ["domain"]=> string(6) "tester" ["email"]=> string(14) "lorem@ipsum.ml" ["password"]=> string(0) "" ["status"]=> string(7) "pending" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) } [2]=> object(App\Subdomain)#191 (23) { ["timestamps"]=> bool(false) ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["attributes":protected]=> array(5) { ["id"]=> int(4) ["domain"]=> string(5) "alpha" ["email"]=> string(14) "alpha@brand.nl" ["password"]=> string(5) "alpha" ["status"]=> string(7) "pending" } ["original":protected]=> array(5) { ["id"]=> int(4) ["domain"]=> string(5) "alpha" ["email"]=> string(14) "alpha@brand.nl" ["password"]=> string(5) "alpha" ["status"]=> string(7) "pending" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) } } }
【问题讨论】:
-
您希望根据什么标准对这些数据进行分组?
-
@Hackerman 的字母。
-
您想按电子邮件地址的首字母分组...
-
@Hackerman 域的第一个字母。如果我有 alpha,那么它属于“a”。
-
现在,你能把
var_dump($data)的输出贴出来