Home > Backend Development > PHP Tutorial > 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
Release: 2023-03-01 15:58:01
Original
1408 people have browsed it

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>
Copy after login
Copy after login

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

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

}
}
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>
Copy after login
Copy after login

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

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

}
}
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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template