Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.4-从每个重复数据行中获取一个数据_Php_Laravel_Sqlite_Laravel 5_Laravel 5.4 - Fatal编程技术网

Php Laravel 5.4-从每个重复数据行中获取一个数据

Php Laravel 5.4-从每个重复数据行中获取一个数据,php,laravel,sqlite,laravel-5,laravel-5.4,Php,Laravel,Sqlite,Laravel 5,Laravel 5.4,我想使用select选项来显示从表的created_at列中获取的月份 // item_ins +----+--------+---------------------+ | id | item | created_at | +----+--------+---------------------+ | 1 | item 1 | 2017-12-11 10:37:52 | | 2 | item 2 | 2017-12-11 10:38:17 | | 3 | item

我想使用select选项来显示从表的created_at列中获取的月份

// item_ins
+----+--------+---------------------+
| id |  item  |     created_at      |
+----+--------+---------------------+
|  1 | item 1 | 2017-12-11 10:37:52 |
|  2 | item 2 | 2017-12-11 10:38:17 |
|  3 | item 3 | 2018-01-12 01:28:43 |
|  4 | item 4 | 2018-01-12 01:30:14 |
|  5 | item 5 | 2018-02-12 01:30:05 |
|  6 | item 6 | 2018-02-12 01:30:42 |
+----+--------+---------------------+
在表中,每两个月是12月、1月和2月,我想从同一个月中得到一个,我试着这样做

$months= ItemIn::distinct('created_at')->pluck('created_at');
但我每个月仍然得到相同的两个月,因为时间不一样,如下所示

我仍然对如何只得到一个月感到困惑,因为按照上面的方法,我也得到了几年和几天

[
    {
        "date": "2017-12-11 10:37:52.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    {
        "date": "2017-12-11 10:38:17.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    {
        "date": "2018-01-12 01:28:43.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    {
        "date": "2018-01-12 01:29:14.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    {
        "date": "2018-02-12 01:30:05.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    {
        "date": "2018-02-12 01:30:22.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
]

使用CAST和原始查询获得您想要的结果,如下所示

$data = \DB::table('item_ins')
        ->select(\DB::raw('CAST(created_at as date) as created_at'))
        ->distinct('created_at')->get();
你可以在这里看到文档


我希望您能理解。

使用CAST和原始查询来获得您想要的结果,如下所示

$data = \DB::table('item_ins')
        ->select(\DB::raw('CAST(created_at as date) as created_at'))
        ->distinct('created_at')->get();
你可以在这里看到文档


我希望您能理解。

如果时间的存在不是问题,您可以在使用select查询本身时处理此问题,如果您正在选择,请使用substring

select id, item, SUBSTRING(created_at, 1, 11) as created_at
from test
group by created_at;

如果时间的存在不是一个问题,您可以在使用select查询本身时处理这个问题,如果您正在选择的话,可以使用substring

select id, item, SUBSTRING(created_at, 1, 11) as created_at
from test
group by created_at;

在视图中使用Carbon对其进行格式化

{{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}
或者在模型中使用以重新设置日期字段的格式

public function getCreatedAtAttribute($value) {
     return \Carbon\Carbon::parse($value)->format('m');
}

然后,
$model->created_at
将采用所需格式

在视图中使用Carbon对其进行格式化

{{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}
或者在模型中使用以重新设置日期字段的格式

public function getCreatedAtAttribute($value) {
     return \Carbon\Carbon::parse($value)->format('m');
}
然后在处创建的
$model->将采用所需格式

使用
groupBy()

使用
groupBy()


选择created_at with date formatting(在创建时使用日期格式),以便省略时间部分。选择created_at with date formatting(在创建时使用日期格式),以便省略时间部分。