ThinkPHP5-- model query returns the object to an array

Using the model returned by the query result set objects, wherein the data inside, the frame will automatically TP5 inside data: protected item processing.

But sometimes we want the data, wanted to return an array can be, and how to do? There are two ways to achieve:

Method One: Find database.php TP5 framework document, the document found in the resultset_type the back of the array into \ think \ Collection [Note] case

// dataset return type 
'resultset_type' => 'array'  ,

Changed

// dataset return type 
'resultset_type' => '\ think \ Collection',

When the query data, followed by the -> toArray (); for example:

$data = User::select() -> toArray();

 

Method two: Set in the model

First, add the attribute in the Model:

protected $resultSetType = 'collection';

When the query data, followed by the -> toArray (); for example:

$data = User::select() -> toArray();
 
 

 

How tp5 output into an array using a js Lane

var ARR = {: json_encode ( $ User )}; 
Console . log (ARR) 
 
outputs the print results are as follows: 
( 2) [{...}, {...}]



Guess you like

Origin www.cnblogs.com/bushui/p/12149649.html