Php 如何在JSON响应中表示MySql zerofill
大家好,我正在尝试检索我的专栏,其中有一个带有json响应的zerofill规范,但似乎php忽略了其中的零,所以我尝试使用Php 如何在JSON响应中表示MySql zerofill,php,mysql,json,laravel,zerofill,Php,Mysql,Json,Laravel,Zerofill,大家好,我正在尝试检索我的专栏,其中有一个带有json响应的zerofill规范,但似乎php忽略了其中的零,所以我尝试使用stru_pad来做与zerofill相同的工作,但它也忽略了它!!! 那么我如何解决这个问题呢? 这是我的密码 public function getGeneralPost(){ $post =Post::inRandomOrder()->orderBy('created_at', 'DESC')->get(); foreach ($post
stru_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);
}
希望这有帮助。当您检索数据时,它已经忽略了零。我认为您需要一个访问器:
function getIdAttribute($value) {
return str_pad($value, 10, '0', STR_PAD_LEFT);
}
希望这能有所帮助。如评论中所述,
id
列似乎有一个默认转换为int
,它将还原您通过stru pad()所做的更改。
为了避免这个问题,您可以将填充的id
存储在一个没有强制转换的单独字段中,也可以不更改对象,而是获取对象的属性,更改它们并使用它们返回结果
仅从中,还可以在返回对象之前覆盖对象的$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);
}
如评论中所述,
id
列似乎有一个默认转换为int
,它将还原通过stru pad()
所做的更改
为了避免这个问题,您可以将填充的id
存储在一个没有强制转换的单独字段中,也可以不更改对象,而是获取对象的属性,更改它们并使用它们返回结果
仅从中,还可以在返回对象之前覆盖对象的$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);
}
我可能错了,但据我所知,在
id
列上有一个默认转换为int
/numeric
,当您将对象序列化为json时,它只会重置您的stru pad
更改。我可能错了,但据我所知,在id
列上有一个默认转换为int
/numeric
,当您将对象序列化为json时,它将简单地重置您的stru pad
更改。谢谢,它工作了,但不是您添加链接的代码,这意味着我在model而不是controllerSorry中添加了键类型,我的回答可能有误。$keyType
属性受保护
,需要通过setter$model->setKeyType('string')
进行设置。我在上面的回答中修正了它我建议您相应地调整您的代码,因为更改主键的类型可能会导致应用程序的其他部分出现问题。谢谢您,它工作正常,但不是您放置的代码您附加的链接,意味着我在模型中添加了键类型而不是控制器。对不起,我的回答可能有误。$keyType
属性受保护
,需要通过setter$model->setKeyType('string')
进行设置。我在上面的回答中修正了它我建议您相应地调整代码,因为更改主键的类型可能会导致应用程序的其他部分出现问题。