Home  >  Article  >  Backend Development  >  laravel Eloquent all() method queries the mysql date field and only returns the year

laravel Eloquent all() method queries the mysql date field and only returns the year

WBOY
WBOYOriginal
2016-12-01 00:25:261366browse

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:

laravel Eloquent all() method queries the mysql date field and only returns the year
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;

laravel Eloquent all() method queries the mysql date field and only returns the year

Using DB::table()->get() can return normally

Reply content:

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:

laravel Eloquent all() method queries the mysql date field and only returns the year
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;

laravel Eloquent all() method queries the mysql date field and only returns the year

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn