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

查看:138
本文介绍了php - laravel Eloquent all()方法查询mysql date字段只返回年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

//
protected $table = 'rpt_ep_read';
protected $primaryKey = 'date_key';

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

 $data = Rpt_ep_read::all(); 
 return $data;

}
}
返回值:


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;

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

解决方案

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

这篇关于php - laravel Eloquent all()方法查询mysql date字段只返回年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆