![](/img/trans.png)
[英]Laravel 5 How to change Date Format from Boostrap datepicker in Controller before Validation
[英]How to Change Date Format From Database In Controller Laravel 7
我尝试在数据库中显示数据,数据库中的日期格式为 2020-10-11T17: 22: 29.000000Z",我怎样才能将其更改为 2020-10-11 或 2020 年 12 月 11 日?
这是我的 controller
public function get_all_artikel(){
$data = ArtikelKonten::select(
'artikel_kategori.nama as kategori','artikel_konten.*')
->join('artikel_kategori','artikel_kategori.id','artikel_konten.id_kategori')
->get();
if ($data){
return response()->json([
'status' => true,
'artikel' => $data,
],200);}
else{
return response()->json([
'status' => false,
'message' => 'No Artikel were found'
],404);}
}
这是我的 model
class ArtikelKonten extends Model
{
protected $table = 'artikel_konten';
protected $fillable = ['id_kategori', 'gambar', 'tag_program', 'nm_program', 'judul', 'preview', 'konten'];
const CREATED_AT = 'created';
const UPDATED_AT = 'modified';
}
这就是结果
{
"status": true,
"artikel": [
{
"kategori": "Program",
"id": 4,
"id_kategori": "2",
"tag_program": "2",
"nm_program": "Zakat Mall",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article1.png",
"judul": "Mengenalkan Zakat Kepada Anak",
"preview": null,
"konten": null,
"created": "2020-12-10T07:24:50.000000Z",
"modified": "2020-12-10T08:06:07.000000Z"
},
{
"kategori": "Berita",
"id": 10,
"id_kategori": "1",
"tag_program": "4",
"nm_program": "Jumat Barokah",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article2.png",
"judul": "Suplemen Iman Ditengah Pandemi",
"preview": null,
"konten": null,
"created": "2020-12-11T20:44:25.000000Z",
"modified": "2020-12-11T20:44:25.000000Z"
},
{
"kategori": "Program",
"id": 11,
"id_kategori": "2",
"tag_program": "2",
"nm_program": "Zakat Mall",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article3.png",
"judul": "Menumbuhkan Semangat Berzakat Umat",
"preview": null,
"konten": null,
"created": "2020-12-11T20:46:23.000000Z",
"modified": "2020-12-11T20:46:23.000000Z"
}
]
}
我只是想将“created”:“2020-12-11T20:46:23.000000Z”更改为“create”:“2020-12-11”,谢谢你们的回答:)
Laravel 7在 Eloquent 型号上使用toArray或toJson方法时使用新的日期序列化格式。
如果您想继续使用以前的行为,您可以覆盖 model 上的serializeDate()
方法:
use DateTimeInterface;
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d');
}
请参阅此处的官方升级文档[7.x]
使用date()
清除日期和时间格式
date('d-m-Y', strtotime($value->created));
Eloquent Model 有一个名为cast
的属性,可以将查询的 output 转换为所需的格式,在您的情况下,只需像在文档中那样转换您的日期列:
class ArtikelKonten extends Model
{
protected $casts = [
'created' => 'datetime:Y-m-d','modified' => 'datetime:Y-m-d'
];
}
Laravel 允许指定日期时间字段在序列化为数组或 json 时应转换的格式
在 model 上定义 $casts 属性
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d',
];
阅读更多: https://laravel.com/docs/8.x/eloquent-mutators#date-casting
你 cab 使用 create_at 的访问器并将此代码放入 model 中:
public function getCreatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
Laravel 框架 | : | 8.42.1 |
---|---|---|
测试日期 | : | 2021 年 5 月 22 日 |
PHP版 | : | 7.3.8 |
数据库 | : | MariaDB 10.5.8 |
操作系统 | : | MacOS 10.13.6 高山脉 |
如何即时转换或更改表列的值/结果,例如将时间戳格式化为日期(ymd h:i:s)格式?
使用Laravel Eloquent:Mutators & Casting通过在 Model 属性中定义数据值来帮助用户即时转换数据值。
当您在 model 实例上检索或设置 Eloquent 属性值时,访问器、修改器和属性转换允许您转换它们。 例如,您可能希望使用 Laravel 加密器加密存储在数据库中的值,然后在 Eloquent Z20F35E630DAF44DBFA4C3F68F5399D8ZC 上访问该属性时自动解密该属性。 或者,当通过 Eloquent model 访问时,您可能希望将存储在数据库中的 JSON 字符串转换为数组。
属性转换提供类似于访问器和修改器的功能,无需您在 model 上定义任何其他方法。 相反,模型的 $casts 属性提供了一种将属性转换为常见数据类型的便捷方法。
<?php
// app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'description'
];
// I've added this because I want to convert timestamp to 'y-m-d' on the fly
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d'
];
}
<?php
// app/Http/Controllers/API/PostController.php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
use Validator;
class PostController extends Controller
{
// all posts, I'm calling this be the default
public function index()
{
$posts = Post::all()->toArray();
return array_reverse($posts);
}
}
<?php
// routes/api.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\PostController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('posts', [PostController::class, 'index']);
Route::group(['prefix' => 'post'], function () {
Route::post('add', [PostController::class, 'add']);
Route::get('edit/{id}', [PostController::class, 'edit']);
Route::post('update/{id}', [PostController::class, 'update']);
Route::delete('delete/{id}', [PostController::class, 'delete']);
});
<template>
<div>
<h3 class="text-center">All Posts</h3><br/>
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Created At</th>
<th>Updated At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="post in posts" :key="post.id">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td style="width: 30%">{{ post.description }}</td>
<td>{{ post.created_at }}</td>
<td>{{ post.updated_at }}</td>
<td>
<div class="btn-group" role="group">
<router-link :to="{name: 'edit', params: { id: post.id }}" class="btn btn-primary">Edit
</router-link>
<button class="btn btn-danger" @click="deletePost(post.id)">Delete</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
posts: []
}
},
created() {
this.axios
.get('/api/posts')
.then(response => {
this.posts = response.data;
});
},
methods: {
deletePost(id) {
this.axios
.delete(`/api/post/delete/${id}`)
.then(response => {
let i = this.posts.map(item => item.id).indexOf(id); // find index of your object
this.posts.splice(i, 1)
});
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.