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

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

Laravel Eloquent API 資源:從響應(yīng)(集合)中刪除“數(shù)據(jù)”鍵

Laravel Eloquent API 資源:從響應(yīng)(集合)中刪除“數(shù)據(jù)”鍵

PHP
翻翻過去那場雪 2022-10-28 15:29:29
我有雄辯的 API 資源UserResource。當(dāng)我嘗試運(yùn)行類似這樣的代碼時:$users = User::paginate(10);return UserResource::collection($users);響應(yīng)將是這樣的:{    "data": [        {            "name": "Fatima Conroy",            "email": "ocie.stark@example.org"        },        {            "name": "John Doe",            "email": "john.doe@example.org"        }    ]}我如何刪除data密鑰或重命名它以獲得類似這樣的響應(yīng)?[    {        "name": "Fatima Conroy",        "email": "ocie.stark@example.org"    },    {        "name": "John Doe",        "email": "john.doe@example.org"    }]
查看完整描述

2 回答

?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊

要獲取所有數(shù)據(jù),只需使用->all()

UserResource::collection($users)->all()

您可以在有關(guān)集合的官方文檔中看到更多信息,其中解釋說 usingall()為您提供集合所表示的底層數(shù)組。


查看完整回答
反對 回復(fù) 2022-10-28
?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

如果您想使用自定義鍵而不是數(shù)據(jù),您可以在資源類上定義 $wrap 屬性:


<?php

    

    namespace App\Http\Resources;

    

    use Illuminate\Http\Resources\Json\JsonResource;

    

    class User extends JsonResource

    {

        /**

         * The "data" wrapper that should be applied.

         *

         * @var string

         */

        public static $wrap = 'user';

    }

如果您想禁用“數(shù)據(jù)”鍵而不是數(shù)據(jù),您可以在資源類上定義 $wrap = null屬性:


<?php

        

        namespace App\Http\Resources;

        

        use Illuminate\Http\Resources\Json\JsonResource;

        

        class User extends JsonResource

        {

            /**

             * The "data" wrapper that should be applied.

             *

             * @var string

             */

            public static $wrap = null;

        }

如果您想禁用最外層資源的包裝,您可以在基礎(chǔ)資源類上使用 withoutWrapping 方法。通常,您應(yīng)該從 AppServiceProvider 或其他服務(wù)提供者調(diào)用此方法,該服務(wù)提供者會在對應(yīng)用程序的每個請求中加載:


<?php


namespace App\Providers;


use Illuminate\Http\Resources\Json\JsonResource;

use Illuminate\Support\ServiceProvider;


class AppServiceProvider extends ServiceProvider

{

    /**

     * Register any application services.

     *

     * @return void

     */

    public function register()

    {

        //

    }


    /**

     * Bootstrap any application services.

     *

     * @return void

     */

    public function boot()

    {

        JsonResource::withoutWrapping(); // This command removes "data" key from all classes extended from "JsonResource"


        user::withoutWrapping(); // This command removes "data" key from only "user"



    }

}

您也可以參考以下官方鏈接了解更多信息: https ://laravel.com/docs/8.x/eloquent-resources#data-wrapping


查看完整回答
反對 回復(fù) 2022-10-28
  • 2 回答
  • 0 關(guān)注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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