1

So I am getting result like this:

[{"modelone_year":2021,"modeltwo_year":2019}]  

How do I need to convert this to this?

[2021,2019]

This is from where I get result:

 $years = Invoice::query()
          ->join('expenses', 'expenses.company_id','=', 'invoices.company_id')
          ->groupBy('modelone_year')
          ->groupBy('modeltwo_year')
          ->get();
2
  • you will convert in javascript or with php Commented Jan 22, 2022 at 10:31
  • yeah with php here Commented Jan 22, 2022 at 10:33

1 Answer 1

1

You got a collection and have to convert to an array. then you can use array_value (https://www.php.net/manual/de/function.array-values.php). You will receive all values as an array: Like that:

<?php
$collection = '[{"modelone_year":2021,"modeltwo_year":2019}]';
$arr = $collection->toArray();

$val = array_values($arr[0]);

print_r($val);
// output [2021,2019]
Sign up to request clarification or add additional context in comments.

2 Comments

Expected parameter of type 'string', 'Illuminate\Database\Eloquent\Collection' provided for json_decode
ok :-) Then you change only one line. instead json_decode use $arr = $collection->toArray();. I updated my answer!

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.