0

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?

2
  • So, if status_category does not exist then you want to show a default value. Am I understanding it right? Commented May 19, 2020 at 4:57
  • Yes if Test is not part of the list of status_categories then i want to add it Commented May 19, 2020 at 20:18

2 Answers 2

1

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

With this solution your issue is resolved or still you facing issue?
Do you get chance in look into this?
0

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 i.e. ??. 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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.