当前位置:Gxlcms > PHP教程 > laravelEloquentall()方法查询mysqldate字段只返回年

laravelEloquentall()方法查询mysqldate字段只返回年

时间:2021-07-01 10:21:17 帮助过:54人阅读

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()能正常返回

回复内容:

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返回

人气教程排行