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

Dev*_*per 6 php laravel

我试图在返回 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]);
    }
Run Code Online (Sandbox Code Playgroud)

请求结果:

 "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.",
                    "icon": "https://picsum.photos/200/300",
                    "image_name": "https://picsum.photos/200/300",
                    "rating": 1,
                    "longitude": -38.117034,
                    "latitude": -32.248637,
                    "availability": 1,
                    "status": 1,
                    "place_type_id": 1,
                    "created_at": "2019-12-14 18:23:19",
                    "updated_at": "2019-12-14 18:23:19"
                }...,
               }
Run Code Online (Sandbox Code Playgroud)

Dev*_*evK 8

将字段添加到$hidden数组中:

// Model

protected $hidden = ['created_at', 'updated_at'];
Run Code Online (Sandbox Code Playgroud)


iam*_*.in 5

您可以使用不同的方法。

方法一:从数据库中只获取必填字段

您可以使用select()方法从数据库中仅检索必需的字段。因此您可以省略不必要的字段。

$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]);

Run Code Online (Sandbox Code Playgroud)

此代码将仅输出父模型 ( placetype) 和子模型 ( places) 中的指定字段。

如果您多次使用这些自定义选择查询并且多次写入所有字段名称很困难,那么您可以使用如下所示的模型范围。

地点类型模型

// add all columns from your table
protected $columns = ['id', 'name', 'icon', 'status', 'created_at', 'updated_at'];

public function scopeExclude($query,$value=[]) 
{
    return $query->select( array_diff( $this->columns,(array) $value) );
}
Run Code Online (Sandbox Code Playgroud)

地点模型

// add all columns from your table
protected $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) );
}
Run Code Online (Sandbox Code Playgroud)

然后您可以删除不需要的字段,如下所示

$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();
Run Code Online (Sandbox Code Playgroud)

礼貌:@Razor 的回答

方法 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);
Run Code Online (Sandbox Code Playgroud)

礼貌:@sajed 的回答

方法3:使用隐藏属性

如果应用程序中的大多数时间都不需要时间戳,您可以使用模型的hidden属性。

地点类型模型地点模型

protected $hidden = ['created_at', 'updated_at'];
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助。