【问题标题】:How to convert my Eloquent Collection to JSON?如何将我的 Eloquent 集合转换为 JSON?
【发布时间】: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)的输出贴出来

标签: php sql json laravel


【解决方案1】:

如果你想使用完整的 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));
});

你应该得到

【讨论】:

  • 嗨,你能帮我看看如何解析吗? ` {{ $json[$char]}}` 来自我的控制器$datax = json_decode($letters-&gt;merge($grouped), true); return view('directories.test', ['json' =&gt; $datax]);
  • 感谢您的回答和想法。已经在视图中解析了。
【解决方案2】:

您不需要 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

【讨论】:

  • @VahnMarty 好吧,这是给你一个想法的示例代码,尝试使用strtolower($char) 而不是$char 或其他东西。如果您想按域名等进行过滤,请尝试将 email 更改为 domain(您的原始问题对于两个数据集具有相同的域)。
  • @VahnMarty 你必须明白,如果他们不知道你收到什么数据,你会得到什么错误,你想用什么列来做搜索等
  • 我正在使用这个但我没有任何结果@foreach ($data-&gt;where('domain', 'like', $char . '%') as $row) {{ $row-&gt;domain }} @endforeach
  • 你给的代码一定有问题,我在控制器里试过了,但没有输出。 ` public function all() { $data = Subdomain::all(); foreach (range('a', 'z') as $char) { echo $char . '
    '; foreach ($data->where('domain', 'like', $char . '%') as $row) { echo $row->domain; } } }`
【解决方案3】:

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

答案就这么简单(Alexey 忘了用 get()),

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

        {{$row->domain}}

        @endforeach

但我想要干净。 所以我在模型(子域)中创建了一个范围。

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>

【讨论】:

  • 这对我来说似乎不对,因为您使用 26 个循环从视图中查询它。可能有更好的方法。
  • @Vahn Marty 我没有忘记get(),当您使用\App\Subdomain::category($char)-&gt;get() 时,每次都会创建新查询。 The Alpha 是对的,您的代码进行了 26 次查询,这非常糟糕。我的代码将只进行一次查询,然后您就可以使用您已经在内存中获得的集合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多