Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is nothing wrong with this</p> <pre><code>Config::set('database.fetch', PDO::FETCH_ASSOC); </code></pre> <p>It should work and it does on my local server. If it's not working for some reason then you can use an alternative way to achieve the same result, i.e.</p> <pre><code>function stdToArray($obj) { if (is_object($obj)) { $obj = get_object_vars($obj); } if (is_array($obj)) { return array_map(__FUNCTION__, $obj); } else { return $obj; } } </code></pre> <p>If you put this function in your <code>filter.php</code> file as a helper function, then you can use it from any where in your <code>app</code> just like</p> <pre><code>$users = DB::table('users')-&gt;get(); dd(stdToArray($users)); </code></pre> <p>The result will be an array of arrays but <code>Config::set('database.fetch', PDO::FETCH_ASSOC);</code> should work and I've checked on my local server, it works just fine.</p> <p><strong>Update :</strong> (Even better, to convert the array of objects to an array of arrays)</p> <pre><code>$users = DB::table('users')-&gt;get(); $users = json_decode(json_encode($users), true); dd($users); // an array of arrays </code></pre> <p><strong>Update :</strong> Why it worked on my local server but not on <code>OP</code>'s server, here it's : (<strong>Thanks to fideloper</strong>)</p> <pre><code>// I have this query at first $users = DB::table('users')-&gt;get(); </code></pre> <p>Then I've following</p> <pre><code>Config::set('database.fetch', PDO::FETCH_ASSOC); $users = DB::table('users')-&gt;get(); dd($users); // expected an array of arrays but it was objects </code></pre> <p>But, if i just remove the first <code>db query</code> then it just works fine with this</p> <pre><code>// $users = DB::table('users')-&gt;get(); Config::set('database.fetch', PDO::FETCH_ASSOC); $users = DB::table('users')-&gt;get(); dd($users); // expected an array of arrays and I get it </code></pre> <p>So, it means that once you make a query and then you use <code>Config::set(...)</code>, it doesn't change the fetch mode because the connection is already made and it's used further. So, this could be the case that, it's not working with <code>Config::set(...);</code>, you probably have make the connection/query. So, the solution is <a href="https://stackoverflow.com/a/19369644/741747">fideloper's answer</a>.</p> <pre><code>DB::connection()-&gt;setFetchMode(PDO::FETCH_ASSOC); $users = DB::table('users')-&gt;get(); dd($users); // an array of arrays </code></pre> <p>Credit goes to <code>fideloper</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload