Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 返回数据时如何禁用或删除更新的\u At和创建的\u At_Php_Laravel - Fatal编程技术网

Php 返回数据时如何禁用或删除更新的\u At和创建的\u At

Php 返回数据时如何禁用或删除更新的\u At和创建的\u At,php,laravel,Php,Laravel,在返回对api的响应之前,我正在尝试删除创建的和更新的。我想从Place\u类型和Places 我该怎么做 我尝试了:unset(placetypes),但没有成功 这是我的代码: public function places() { $placeType = PlaceType::with('places')->where('id', 1)->get(); return response()->json(['placeTyp

在返回对api的响应之前,我正在尝试删除创建的和更新的。我想从Place\u类型和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.",
                    "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"
                }...,
               }
1) 您只需要声明
public$timestamps=false在每个要隐藏它的模型中

2) 您还可以通过从迁移中删除
$table->timestamps()
来禁用时间戳

3) 声明
protected$hidden=['created_at','updated_at']在您的模型中。

将字段添加到数组中:

//模型
受保护的$hidden=[“已创建”、“已更新”];

如果您不想要这些列,您可以根据其他人所说的(在“时间戳”标题下)


但是,如果您需要这些列,而只是不希望它们出现在json响应中,那么可以使用
resource
。如前所述,如果要从模型中删除时间戳,请参见。

,将其放置在模型中:

public $timestamps = false;

还可以使用up()方法中的以下代码创建迁移并运行它:

Schema::table('your_table', function (Blueprint $table) {
    $table->dropTimestamps();
});
您可以在down()方法中使用$table->timestamps()来允许回滚。 还是在模型中

const UPDATED_AT = null;
const CREATED_AT = null;

假设$placeType为array,则可以使用此递归函数:

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;
}

您可以使用不同的方法

方法1:仅从数据库中获取所需字段

您可以使用
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]);

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

如果您多次使用这些定制的select查询,并且很难多次写入所有字段名,那么您可以使用如下所示的模型范围

PlaceType型号

// 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) );
}
// 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) );
}
protected $hidden = ['created_at', 'updated_at'];
放置模型

// 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) );
}
// 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) );
}
protected $hidden = ['created_at', 'updated_at'];
然后可以删除不需要的字段,如下所示

$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回答

方法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

方法3:使用隐藏属性

如果时间戳在应用程序中的大部分时间都是不必要的,您可以使用模型的
hidden
属性

场所类型模型场所模型

// 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) );
}
// 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) );
}
protected $hidden = ['created_at', 'updated_at'];

希望这会有帮助。我这样做了,但它仍然返回更新的和创建的。在模型
Place\u-Type
Places
@developer中都执行它,如果需要,但不想显示它们,请在模型中的$hidden中设置它们<代码>受保护的$hidden=['created_at','updated_at']@developer这必须适用于任何模型查询。请使用
protected$hidden=['created_at','updated_at']placeType
places
模型中都有code>。