如何将我的Eloquent Collection转换为JSON?

时间:2016-11-30 15:59:02

标签: php sql json laravel

我的数据库输出是:

[{"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) enter image description here

这是来自我的控制器:

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

3 个答案:

答案 0 :(得分:2)

如果你想进入Full Laravel,请使用收藏的力量:

Route::get('/json', function() {
    $json = '[{"id":1,"domain":"test","email":"test@mail.com","status":"pending"},{"id":3,"domain":"tester","email":"lorem@ipsum.ml","status":"pending"}]';

    $letters = collect(array_combine(range('a', 'z'), array_fill(1, 26, [])));

    $grouped = collect(json_decode($json))->groupBy(function ($item) {
        return $item->email[0];
    });

    dd($letters->merge($grouped));
});

你应该得到

enter image description here

答案 1 :(得分:1)

您不需要JSON,而是获得$data = Subdomain::all();的集合。只需使用where()like一起搜索电子邮件以字符开头的所有行:

@foreach (range('A', 'Z') as $char)
    // Some HTML here.
    @foreach ($data->where('email', 'like', $char) as $row)
        {{ $data->domain}} {{ $data->email }}
    @endforeach
    // Some HTML here.
@endforeach

答案 2 :(得分:0)

我得到了答案。感谢@alexey将查询放入刀片模板的想法。

答案比较简单(Alexey忘了使用get()),

  @foreach ($data->where('domain', 'like', $char . '%)->get() as $row)

        {{$row->domain}}

        @endforeach

但我想要干净。 所以我在Model(Subdomain)中创建了一个范围。

public function scopeCategory($query, $char)
    {
      $query->where('domain', 'like',  $char . '%')->get();
    }

并在我的刀片视图中执行此操作:

 <div class="panel-body">
        @foreach (\App\Subdomain::category($char)->get() as $row)

        {{$row->domain}}

        @endforeach


      </div>