Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.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 如何在JSON响应中表示MySql zerofill_Php_Mysql_Json_Laravel_Zerofill - Fatal编程技术网

Php 如何在JSON响应中表示MySql zerofill

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

大家好,我正在尝试检索我的专栏,其中有一个带有json响应的zerofill规范,但似乎php忽略了其中的零,所以我尝试使用
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')
进行设置。我在上面的回答中修正了它我建议您相应地调整代码,因为更改主键的类型可能会导致应用程序的其他部分出现问题。