第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

返回數(shù)據(jù)時如何禁用或刪除 updated_At 和 Created_at

返回數(shù)據(jù)時如何禁用或刪除 updated_At 和 Created_at

PHP
慕工程0101907 2022-06-17 15:59:25
我試圖在返回對 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.",                }...,               }
查看完整描述

5 回答

?
忽然笑

TA貢獻1806條經(jīng)驗 獲得超5個贊

如果您想從模型中刪除時間戳,如前所述,請將其放在您的模型中:


public $timestamps = false;


還要在 up() 方法中使用以下代碼創(chuàng)建遷移并運行它:


Schema::table('your_table', function (Blueprint $table) {

    $table->dropTimestamps();

});

您可以在 down() 方法中使用 $table->timestamps() 來允許回滾?;蛟谀P椭?/p>


const UPDATED_AT = null;

const CREATED_AT = null;


查看完整回答
反對 回復 2022-06-17
?
呼啦一陣風

TA貢獻1802條經(jīng)驗 獲得超6個贊

將字段添加到$hidden數(shù)組中:


// Model


protected $hidden = ['created_at', 'updated_at'];


查看完整回答
反對 回復 2022-06-17
?
DIEA

TA貢獻1820條經(jīng)驗 獲得超2個贊

您可以使用不同的方法。


方法 1:從數(shù)據(jù)庫中僅獲取必填字段


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

}

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


$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()方法從序列化中隱藏您的列。在此方法中,在獲取包含所有字段的行后,您將指定的字段設為隱藏。[請注意,排除的變量不會出現(xiàn)在轉儲上,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'];

希望這會有所幫助。??


查看完整回答
反對 回復 2022-06-17
?
嚕嚕噠

TA貢獻1784條經(jīng)驗 獲得超7個贊

1)您只需要public $timestamps = false;在要隱藏它的每個模型中聲明即可。

2)您還可以通過$table->timestamps()從遷移中刪除來禁用時間戳。

3)protected $hidden = ['created_at', 'updated_at'];在你的模型中聲明。


查看完整回答
反對 回復 2022-06-17
?
牧羊人nacy

TA貢獻1862條經(jīng)驗 獲得超7個贊

假設 $placeType 是數(shù)組,你可以使用這個遞歸函數(shù):


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;

}


查看完整回答
反對 回復 2022-06-17
  • 5 回答
  • 0 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號