-2

Here is my Query

$reminderData = Reminder::where('status', 1)->get()->toArray();

Here is my Result

[
{"id":1,"reminder_type":2,"reminder_name":"first","reminder_date":"2016-10-26"},
{"id":2,"reminder_type":2,"reminder_name":"second","reminder_date":"2016-10-26"},
{"id":3,"reminder_type":2,"reminder_name":"third","reminder_date":"2016-10-26"},
]

But i need like

[
["id":1,"reminder_type":2,"reminder_name":"first","reminder_date":"2016-10-26"],
["id":2,"reminder_type":2,"reminder_name":"second","reminder_date":"2016-10-26"],
["id":3,"reminder_type":2,"reminder_name":"third","reminder_date":"2016-10-26"],
]

How can i do this ?

12
  • 2
    You're showing JSON there, not an array. Where are you converting to JSON? And the desired output is not valid JSON. Commented Oct 26, 2016 at 13:11
  • 1
    No, it's not. Your "result" is a json string, that's not what Eloquent toArray() provides. Commented Oct 26, 2016 at 13:14
  • 1
    Do this: dd(Reminder::where('status', 1)->get()->toArray());. Commented Oct 26, 2016 at 13:15
  • 1
    You need to show more of your code. Somewhere you are outputting the array as JSON and you are confused. If you try that dd code I gave you, you'll see you absolutely have an array from Eloquent, not json. Commented Oct 26, 2016 at 13:16
  • 2
    Excellent. So: you see you have an array of arrays. Which is exactly what you wanted. Your code is fine, it's already giving you the structure you are asking for. Commented Oct 26, 2016 at 13:27

4 Answers 4

1

It actually returns array of arrays. You you'll use dd() on both levels, you'll see an array data type.

Also, you can get only columns you need with ->get(['id', 'reminder_type', 'reminder_date'])

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

8 Comments

ah, i am getting this as my result now {"2":1,"1":2,"3":3}
@SA__, oh yeah, sorry. Fixed.
Ohh.. that's ok .. Stilll i am getting like this [{"id":1,"reminder_type":2,"reminder_date":"2016-10-26"},{"id":2,"reminder_type":1,"reminder_date":"2016-10-26"},{"id":3,"reminder_type":3,"reminder_date":"0000-00-00"}]
ah, i tried in your way.. not sure, why it is not giving the expected behaviour. btw i am using laravel 5.1
@SA__ try to do dd($reminderData ) and you'll see an array data type. Then try to do dd($reminderData[0]) and it will show you a type of one row which will be also array.
|
0
$reminderData = Reminder::where('status', 1)->pluck('column_name1', 'column_name2')

In the laravel 5.0 and above version "pluck" method return data in the array format

Comments

0

Try this one

\Illuminate\Support\Facades\DB::setFetchMode(PDO::FETCH_ASSOC);
$values=\Illuminate\Support\Facades\DB::select("select * from  your_table");
\Illuminate\Support\Facades\DB::setFetchMode(PDO::FETCH_CLASS);
var_dump($values);

Comments

0

As wrote in comments:

it looks like dirty hack and it can cause unexpected behavior with more complicated data


You can return

 json_decode(Reminder::where('status', 1)->get(), true);

as already asked here

4 Comments

why on Earth you want to json_decode an Eloquent collection?
Never, but the question is to return an array of array, and it does.
The default array structure returned by Laravel is similar to json formatted arrays, so in order to let PHP convert it to standard array, you must use json_decode method
@DoaaMagdy, it doesn't look like json at all. If you see {}, it doesn't mean it's json. It's completely different data type. The code above will give you an error.

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.