Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toAssoc returns a single array when result has 1 result #17

Closed
levizwannah opened this issue May 10, 2023 · 2 comments
Closed

toAssoc returns a single array when result has 1 result #17

levizwannah opened this issue May 10, 2023 · 2 comments

Comments

@levizwannah
Copy link

I noticed that the Result->toAssoc() function returns a single array when the result has 1 row. That is, the array returned is not an array of associative arrays. So it makes it inconsistent when looping over the values. For example.

DB::table('test')->where('name', 1)->get()->toAssoc(); // returns
// [ 'name' => 'test ]

// next
DB::table('test')->get()->toAssoc(); // returns
//[ [ 'name' => 'test'], ['name' => 'test2'], ... ]
  
@muhammetsafak
Copy link
Member

Thanks for your return. I will deal with it as soon as possible.

muhammetsafak added a commit that referenced this issue Nov 7, 2023
@muhammetsafak
Copy link
Member

The behavior of methods such as toAssoc() or toArray() may vary depending on the number of rows returned. Instead, with methods such as asAssoc() or asArray() that only change the data type to be returned; You can use row() or rows() methods.

Example :

DB::table('test')->get()->asAssoc()->row(); // returns
// [ 'name' => 'test ]

DB::table('test')->get()->asAssoc()->rows(); // returns
//[ [ 'name' => 'test'], ['name' => 'test2'], ... ]
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants