如何在 JSON 响应中表示 MySql zerofill
how to represent MySql zerofill in JSON response
大家好,我正在尝试检索我的专栏,该专栏具有 json 响应的 zerofill 规范,但似乎 php 忽略了其中的零,所以我尝试使用 str_pad
做与 zerofill 相同的工作,但它也会忽略它!!!
那么我该如何解决这个问题呢?
这是我的代码
public function getGeneralPost(){
$post =Post::inRandomOrder()->orderBy('created_at', 'DESC')->get();
foreach ($post as $item){
$categories = Category::where('id' , $item->post_subtype)->select('category_title')->first();
$categories_type = Category::where('id' , $item->post_type)->select('category_title')->first();
$item->post_subtype = $categories->category_title;
$item->post_type = $categories_type->category_title;
$item->id = str_pad($item->id ,10,'0',STR_PAD_LEFT);// here I am usting str_pad
}
$success['posts'] =$post;
return response()->json(['code'=>'success','success' => $success], $this->successStatus);
}
当您检索数据时,它已经忽略了零。我认为您需要一个访问器:
function getIdAttribute($value) {
return str_pad($value, 10, '0', STR_PAD_LEFT);
}
希望对您有所帮助。
如评论中所述,id
列似乎默认转换为 int
,这将恢复通过 str_pad()
所做的更改。
为了避免这个问题,您可以将填充后的 id
存储在一个没有强制转换的单独字段中,或者,您可以获取对象的属性,而不是更改对象,更改它们并将它们用于return你的结果。
仅从 framework code itself 开始,也可以在 return 之前覆盖对象的 $keyType
属性:
public function getGeneralPost() {
$post = Post::inRandomOrder()->orderBy('created_at', 'DESC')->get();
foreach ($post as $item) {
$categories = Category::where('id' , $item->post_subtype)->select('category_title')->first();
$categories_type = Category::where('id' , $item->post_type)->select('category_title')->first();
$item->post_subtype = $categories->category_title;
$item->post_type = $categories_type->category_title;
$item->setKeyType('string');
$item->id = str_pad($item->id ,10,'0',STR_PAD_LEFT);
}
$success['posts'] = $post;
return response()->json(['code'=>'success','success' => $success], $this->successStatus);
}
大家好,我正在尝试检索我的专栏,该专栏具有 json 响应的 zerofill 规范,但似乎 php 忽略了其中的零,所以我尝试使用 str_pad
做与 zerofill 相同的工作,但它也会忽略它!!!
那么我该如何解决这个问题呢?
这是我的代码
public function getGeneralPost(){
$post =Post::inRandomOrder()->orderBy('created_at', 'DESC')->get();
foreach ($post as $item){
$categories = Category::where('id' , $item->post_subtype)->select('category_title')->first();
$categories_type = Category::where('id' , $item->post_type)->select('category_title')->first();
$item->post_subtype = $categories->category_title;
$item->post_type = $categories_type->category_title;
$item->id = str_pad($item->id ,10,'0',STR_PAD_LEFT);// here I am usting str_pad
}
$success['posts'] =$post;
return response()->json(['code'=>'success','success' => $success], $this->successStatus);
}
当您检索数据时,它已经忽略了零。我认为您需要一个访问器:
function getIdAttribute($value) {
return str_pad($value, 10, '0', STR_PAD_LEFT);
}
希望对您有所帮助。
如评论中所述,id
列似乎默认转换为 int
,这将恢复通过 str_pad()
所做的更改。
为了避免这个问题,您可以将填充后的 id
存储在一个没有强制转换的单独字段中,或者,您可以获取对象的属性,而不是更改对象,更改它们并将它们用于return你的结果。
仅从 framework code itself 开始,也可以在 return 之前覆盖对象的 $keyType
属性:
public function getGeneralPost() {
$post = Post::inRandomOrder()->orderBy('created_at', 'DESC')->get();
foreach ($post as $item) {
$categories = Category::where('id' , $item->post_subtype)->select('category_title')->first();
$categories_type = Category::where('id' , $item->post_type)->select('category_title')->first();
$item->post_subtype = $categories->category_title;
$item->post_type = $categories_type->category_title;
$item->setKeyType('string');
$item->id = str_pad($item->id ,10,'0',STR_PAD_LEFT);
}
$success['posts'] = $post;
return response()->json(['code'=>'success','success' => $success], $this->successStatus);
}