6

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
    }  
}
}
4
  • Same as product, you can use favouritepost with with function. Commented Jul 5, 2018 at 7:37
  • what if i want $favouritePost use this ? Commented Jul 5, 2018 at 7:38
  • 2
    @Javed its should be as simple as adding a new property on the $addedProduct object. $addedPost->favouritepost = $favouritePost have you tried this? Commented Jul 5, 2018 at 8:33
  • @OluwatobiSamuelOmisakin yes it is working perfect. Commented Jul 5, 2018 at 8:51

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Union possibly would make a new query on the addedPost instance of Query builder since $addedPost is not a collection, see the first() method used after the first query. I tested it on Laravel 5.4 and I want to be sure this is not some strange behavior that is fixed on L5.6
sorry I heavily edited your answer to effect the right example you may be referring to. I hope I didnt change any of your idea in the process.
I observe that the first() return a Builder class instead of StdClass although indeed StdClass is close
Interesting. Laravel Doc tells that it returns a StdClass instance. Gotta check that later. Edit: Maybe Builder behaves like a StdClass object and thats why
It'd return StdClass if You use the DB facade and then you cannot chain any other method to the first object. But if you use Eloquent (wrapped around query builder as well), then you have an instance of the Model that can further do other operations like ->load('myOtherRelation') - Lazy Loading. I may not be entirely correct too. It'd be an interesting topic to find out.
|
0

first you store all the values which you are returning in one associative array then use array_push() to add new key/value in that array.After all you just return that array.

1 Comment

$existing_array = array('status_code' => $status_code, 'message' => $message, 'PostDetails' => $addedPost,'favouritePost' => $favouritePost); return $existing_array;
0

Assuming that $addedPost is a laravel collection you can simply do:

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

$addedPost->put('favouritepost', $favouritePost);

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

1 Comment

See the last method call on $adedPost is ->first() hence it cannot be a collection most likely its Query Builder and method put() does not exist on Query Builder except there's difference now in L 5.6 (tested this on L5.4 though)
0

for adding key value in object

$object->new_key="value";

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.