【问题标题】:Add new object key value to laravel db query results向 laravel db 查询结果添加新的对象键值
【发布时间】: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


【解决方案1】:

对象键值的设置需要使用laravel集合map函数。

$categories = $categories->map(function ($item, $key) {
     $item->status_category = "test"; 
     return $item;
});

【讨论】:

  • 通过此解决方案,您的问题已解决还是您仍然面临问题?
  • 你有机会调查一下吗?
【解决方案2】:

可以通过

实现

1) Laravel 合集map()

$labTestRequests = $labTestRequests->map(function($labTestRequest){
    if(!isset($labTestRequest->status_category)) {   // checking if a key exist
        $labTestRequest->status_category = "default";
    }
    return $labTestRequest;
});

2) 使用简单的null coalesce 运算符,即??。它的作用是如果给定变量不存在为空,则使用给定的default代替。在你看来你可以这样写

$labTestRequest->status_category ?? 'default'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多