2

I'm having a data set which is coming through request in my Laravel 6 application, I want to convert this to collection but it is not working out:

$request_status = json_decode($request->status);

$data = collect($request_status)->pluck('id');

dd($data);

This is giving me output of null;

When I do dd($request->status):

"[{"id":3,"type":"Awarded","name":"Awarded"}]"

No change happens in the data, if I do dd($data) I get:

Collection {#791 ▼
  #items: array:1 [▼
    0 => null
  ]
}

I tried doing json_decode($request->status, true) but no luck.

3
  • "[{"id":3,"type":"Awarded","name":"Awarded"}]" will be error. Are you sure this is the value of $request->status? Commented Dec 19, 2019 at 15:17
  • Try to change '[{"id":3,"type":"Awarded","name":"Awarded"}]', it can run normally. Commented Dec 19, 2019 at 15:19
  • what you are getting for gettype($request) ? Commented Dec 20, 2019 at 7:37

2 Answers 2

1

If you dd $request_status it returns

"[{"id":3,"type":"Awarded","name":"Awarded"}]"

as a string? Makes sense that the pluck('id') doesn't work then. If so, make sure that returns an array and it'll work.

When I try

$request_status = json_decode('[{"id":3,"type":"Awarded","name":"Awarded"}]');

$data = collect($request_status)->pluck('id');

dd($data);

it returns

Illuminate\Support\Collection^ {#631
  #items: array:1 [
    0 => 3
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure about correctness but, I have tried something

$request_status = json_decode($request->status,1);

$data = collect([$request_status])->pluck('id')->first();

dd($data);

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.