Home >Backend Development >PHP Tutorial >laravel Eloquent all() method queries the mysql date field and only returns the year
Mysql date_key field attribute is date,
After creating the model with laravel eloquent and using the all() method, the date_key field only returns the year.
I don’t know if it’s the model time format setting or something else, please give me some advice. The code is as follows:
Model:
class Rpt_ep_read extends Model
{
<code>// protected $table = 'rpt_ep_read'; protected $primaryKey = 'date_key';</code>
}
Controller
use AppModelsRptdataWebRpt_ep_read;
class TestController extends Controller {
public function test(){
<code> $data = Rpt_ep_read::all(); return $data;</code>
}
}
Return value:
mysql
CREATE TABLE rpt_ep_read
(date_key
date NOT NULL COMMENT 'time',cust_num
int(11) NOT NULL COMMENT 'number of users',
PRIMARY KEY (date_key
)
) ENGINE =InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Using DB::table()->get() can return normally
Mysql date_key field attribute is date,
After creating the model with laravel eloquent and using the all() method, the date_key field only returns the year.
I don’t know if it’s the model time format setting or something else, please give me some advice. The code is as follows:
Model:
class Rpt_ep_read extends Model
{
<code>// protected $table = 'rpt_ep_read'; protected $primaryKey = 'date_key';</code>
}
Controller
use AppModelsRptdataWebRpt_ep_read;
class TestController extends Controller {
public function test(){
<code> $data = Rpt_ep_read::all(); return $data;</code>
}
}
Return value:
mysql
CREATE TABLE rpt_ep_read
(date_key
date NOT NULL COMMENT 'time',cust_num
int(11) NOT NULL COMMENT 'number of users',
PRIMARY KEY (date_key
)
) ENGINE =InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Using DB::table()->get() can return normally
Add public $incrementing = false;
below your Model
, otherwise Laravel
’s default primary key is auto-incremented and will be converted to int
and returned