返回数据时如何禁用或删除 updated_At 和 Created_at

我试图在返回对 api 的响应之前删除 created_at 和 updated_at 。我想从Place_Type 和 Places中删除这些字段, 我该怎么做?: 我试过: unset(placetypes) 但没用


这是我的代码:


    public function places()

    {


        $placeType = PlaceType::with('places')->where('id', 1)->get();


        return response()->json(['placeType' => $placeType]);

    }

请求结果:


 "placeType": [

        {

            "id": 1,

            "name": "Moriah O'Conner",

            "icon": "https://picsum.photos/200/300",

            "status": 1,

            "created_at": "2019-12-14 18:23:19",

            "updated_at": "2019-12-14 18:23:19",

            "places": [

                {

                    "id": 1,

                    "name": "Linda Leffler",

                    "description": "Alice's shoulder, and it set to work, and very soon came to ME, and told me he was in the air. She did it so VERY remarkable in that; nor did Alice think it would feel very queer to ME.' 'You!' said.",

                    "icon": "https://picsum.photos/200/300",

                    "image_name": "https://picsum.photos/200/300",

                    "rating": 2,

                    "longitude": -53.389979,

                    "latitude": 19.633458,

                    "availability": 1,

                    "status": 1,

                    "place_type_id": 1,

                    "created_at": "2019-12-14 18:23:19",

                    "updated_at": "2019-12-14 18:23:19"

                },

                {

                    "id": 2,

                    "name": "Lauren Cartwright",

                    "description": "I should say \"With what porpoise?\"' 'Don't you mean by that?' said the King. 'I can't remember half of anger, and tried to look at it!' This speech caused a remarkable sensation among the leaves.",

                }...,

               }


慕工程0101907
浏览 229回答 5
5回答

忽然笑

如果您想从模型中删除时间戳,如前所述,请将其放在您的模型中:public $timestamps = false;还要在 up() 方法中使用以下代码创建迁移并运行它:Schema::table('your_table', function (Blueprint $table) {    $table->dropTimestamps();});您可以在 down() 方法中使用 $table->timestamps() 来允许回滚。或在模型中const UPDATED_AT = null;const CREATED_AT = null;

呼啦一阵风

将字段添加到$hidden数组中:// Modelprotected $hidden = ['created_at', 'updated_at'];

DIEA

您可以使用不同的方法。方法 1:从数据库中仅获取必填字段您可以使用select()方法从 db 中仅检索必填字段。因此,您可以省略不必要的字段。$placeType = PlaceType::with(['places'  => function ($query) {                    $query->select('id', 'name', 'description', 'icon',                        'image_name', 'rating', 'longitude', 'latitude',                        'availability', 'status', 'place_type_id'); //timestamps excluded                }])                ->select('id', 'name', 'icon', 'status') //timestamps excluded                ->where('id', 1)                ->get();return response()->json(['placeType' => $placeType]);此代码将仅输出父模型 ( placetype) 和子模型 ( places) 中的指定字段。如果您多次使用这些自定义选择查询并且多次写入所有字段名称很困难,那么您可以使用如下模型范围。PlaceType 模型// add all columns from your tableprotected $columns = ['id', 'name', 'icon', 'status', 'created_at', 'updated_at'];public function scopeExclude($query,$value=[]) {    return $query->select( array_diff( $this->columns,(array) $value) );}放置模型// add all columns from your tableprotected $columns = ['id', 'name', 'description', 'icon', 'image_name',                        'rating', 'longitude', 'latitude', 'availability',                        'status', 'place_type_id', 'created_at', 'updated_at'                    ];public function scopeExclude($query,$value=[]) {    return $query->select( array_diff( $this->columns,(array) $value) );}然后您可以删除不需要的字段,如下所示$placeType = PlaceType::with(['places' => function ($query) {                    $query->exclude(['created_at', 'updated_at']); //exclude fields from Place model                }])                ->exclude(['created_at', 'updated_at']) //exclude fields from PlaceType model                ->where('id', 1)                ->get();礼貌:@Razor 的这个SO 回答方法2:在您需要的地方隐藏您的列您可以使用 laravel 的makeHidden()方法从序列化中隐藏您的列。在此方法中,在获取包含所有字段的行后,您将指定的字段设为隐藏。[请注意,排除的变量不会出现在转储上,json但可能在转储上可见]。//get rows with all fileds (except hidden)$placeType = PlaceType::with('places')->where('id', 1)->get();//making timestamps hidden in child model's rows$placeType->places->makeHidden(['created_at','updated_at']);//making timestamps hidden in parent model's rows$placeType->makeHidden(['created_at','updated_at']);return response()->json($placeType);礼貌:@sajed 的这个SO 回答方法3:使用隐藏属性如果在应用程序中的大部分时间都不需要时间戳,您可以使用模型的hidden属性。PlaceType 模型和放置模型protected $hidden = ['created_at', 'updated_at'];希望这会有所帮助。🤗

噜噜哒

1)您只需要public $timestamps = false;在要隐藏它的每个模型中声明即可。2)您还可以通过$table->timestamps()从迁移中删除来禁用时间戳。3)protected $hidden = ['created_at', 'updated_at'];在你的模型中声明。

牧羊人nacy

假设 $placeType 是数组,你可以使用这个递归函数:function removeTimeStampValues($array) {    if(array_key_exists('created_at', $array) && array_key_exists('updated_at', $array)) {       unset($array['created_at']);       unset($array['updated_at']);    }    foreach ($array as $key => $value) {       if(is_array($value)) {         $array[$key] = recursiveRemoveTimeStampValue($value);       }    }    return $array;}
打开App,查看更多内容
随时随地看视频慕课网APP