Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LARAVEL : Add key and value in single array

I have a single array of data, I want to add a key and it's value in same array . Here in addedPost I want to add key favouritePost and it's value is $favouritePost after product key. How can i do this ?

Here is my query:

$addedPost      =   Post::with(['product','postattribute.attribute.category','user.userDetails'])
                ->whereId($postData['post_id'])
                ->first();
    $favouritePost  = PostFavourite::isAlreadyAdded($postData['post_id'], Auth::id());

    return  [
       'status_code'     =>    $status_code,
       'message'         =>    $message,
       'PostDetails'     =>    $addedPost
    ];

What I get in response :

{
"PostDetails": {
    "id": 289,
    "user_id": 12,
    "product_id": 2,
    "demand_or_supply": "Demand",
    "description": "edited1",
    "status": "Expired",
    "created_at": "2018-06-22 07:35:27",
    "updated_at": "2018-07-05 06:42:56",
    "product": {
        "id": 2,
        "title": "Diamond",
        "icon": null,
        "status": "Active"
    } 
}
}

EXPECTED RESULT:

{
"PostDetails": {
    "id": 289,
    "user_id": 12,
    "product_id": 2,
    "demand_or_supply": "Demand",
    "description": "edited1",
    "status": "Expired",
    "created_at": "2018-06-22 07:35:27",
    "updated_at": "2018-07-05 06:42:56",
    "product": {
        "id": 2,
        "title": "Diamond",
        "icon": null,
        "status": "Active"
    },
    "favouritepost": {
         "id": 8,
         "post_id": 289,
         "user_id": 12
    }  
}
}
like image 771
Javed Avatar asked Jul 05 '18 07:07

Javed


People also ask

What is -> in laravel?

-> and => are both operators. The difference is that => is the assign operator that is used while creating an array. For example: array(key => value, key2 => value2) And -> is the access operator. It accesses an object's value.

How do you push a key and value in an array?

Answer: Use the Square Bracket [] Syntax php // Sample array $array = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Adding key-value pairs to an array $array["d"] = "Dog"; $array["e"] = "Elephant"; print_r($array); ?>

How do you add a key to an array?

To add a key/value pair to all objects in an array:Use the Array. map() method to iterate over the array. On each iteration, use the spread syntax to add the key/value pair to the current object. The key/value pair will get added to all objects in the new array.


1 Answers

First: Your $addedPost is not an array but a Eloquent Collection. There are multiple possibilites to do this. The easiest one is to union an Array with the Collection.

$union = $addedPost->union($favouritePost->toArray());

For every other solution please take a look at the Laravel Documentation. It's pretty easy to understand.

https://laravel.com/docs/5.6/collections

Edit: Though I missed the ->first() inside the question just use the solution already mentioned. ->first() returns a StdClass Object, so you can handle it like it:

$addedPost->favouritepost = $favouritePost;

That property favouritePost is added to $addedPost object in that case. There's no need for any method call again.

like image 172
demo Avatar answered Oct 12 '22 10:10

demo