【发布时间】:2020-09-05 01:35:43
【问题描述】:
我有一个查询,它返回一个像这样的集合对象:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Old Results
)
[original:protected] => Array
(
[status_category] => Old Results
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[1] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Pending
)
[original:protected] => Array
(
[status_category] => Pending
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[2] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Done
)
[original:protected] => Array
(
[status_category] => Done
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[3] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Non-Negative
)
[original:protected] => Array
(
[status_category] => Non-Negative
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[4] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Test Complete Pending Review
)
[original:protected] => Array
(
[status_category] => Test Complete Pending Review
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
)
)
在我看来,迭代每个并显示 status_category 值 - 我试图完成的是检查特定 status_category 值是否不存在,然后将其添加到末尾。 我试过做: $categories->prepend('test', 'status_category');
但这并没有像上面的对象那样添加它,因此我的视图会引发错误。对此有何想法?
【问题讨论】:
-
因此,如果 status_category 不存在,那么您希望显示默认值。我理解对了吗?
-
是的,如果 Test 不是 status_categories 列表的一部分,那么我想添加它
标签: php laravel laravel-5 eloquent laravel-collection