简体   繁体   中英

Add new object key value to laravel db query results

I have a query that returns a collection object like so:

 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] => 
            )

    )

  )

In my view im iterating through each and showing the status_category value - what im trying to accomplish is to check if a specific status_category value does not exist then to add it to the end. I have tried doing: $categories->prepend('test', 'status_category');

but that doesnt add it like the object above and therefore my view will throw an error. Any ideas on this?

You need to use the laravel collection map function for set value in object key value.

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

You can achieve via

1) Laravel Collection map()

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

2) Using a simple null coalesce operator ie ?? . What it does is if a given variable does not exists or is null then use the given default instead. In your view you can write like this

$labTestRequest->status_category ?? 'default'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM