首页  >  文章  >  后端开发  >  laravel Eloquent all()方法查询mysql date字段只返回年

laravel Eloquent all()方法查询mysql date字段只返回年

WBOY
WBOY原创
2016-12-01 00:25:261365浏览

mysql date_key 字段属性是date,
用laravel eloquent创建模型后 使用all() 方法,date_key 字段只返回年。
不知道是模型时间格式得设置还是什么问题,求指点。代码如下:
模型:
class Rpt_ep_read extends Model
{

<code>//
protected $table = 'rpt_ep_read';
protected $primaryKey = 'date_key';</code>

}
控制器
use AppModelsRptdataWebRpt_ep_read;
class TestController extends Controller {
public function test(){

<code> $data = Rpt_ep_read::all(); 
 return $data;</code>

}
}
返回值:

laravel Eloquent all()方法查询mysql date字段只返回年
mysql
CREATE TABLE rpt_ep_read (
date_key date NOT NULL COMMENT '时间',
cust_num int(11) NOT NULL COMMENT '用户数',
PRIMARY KEY (date_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

laravel Eloquent all()方法查询mysql date字段只返回年

用DB::table()->get()能正常返回

回复内容:

mysql date_key 字段属性是date,
用laravel eloquent创建模型后 使用all() 方法,date_key 字段只返回年。
不知道是模型时间格式得设置还是什么问题,求指点。代码如下:
模型:
class Rpt_ep_read extends Model
{

<code>//
protected $table = 'rpt_ep_read';
protected $primaryKey = 'date_key';</code>

}
控制器
use AppModelsRptdataWebRpt_ep_read;
class TestController extends Controller {
public function test(){

<code> $data = Rpt_ep_read::all(); 
 return $data;</code>

}
}
返回值:

laravel Eloquent all()方法查询mysql date字段只返回年
mysql
CREATE TABLE rpt_ep_read (
date_key date NOT NULL COMMENT '时间',
cust_num int(11) NOT NULL COMMENT '用户数',
PRIMARY KEY (date_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

laravel Eloquent all()方法查询mysql date字段只返回年

用DB::table()->get()能正常返回

在你的Model下面加上public $incrementing = false; 不然Laravel 默认主键是自增的,会转成int返回

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn