Lumen 5.6.4 API资源集合不返回所有数据

问题描述 投票:0回答:1

我有一个返回json的查询,我将其传递给资源集合。问题是它正确地返回所有字段但是当它来到companies_closed字段时它显示为null。而companies_closed是数组或空数组。我不明白为什么会这样。

我在Laravel 5.6.39中使用了同样的方法(通过一堆函数传递数据),它在Laravel中运行得很好。但是当我在流明5.6.4中这样做时,它不起作用。

我认为这可能是缓存问题,所以我几乎遵循了这个网址中的所有方法但没有成功。因为它不起作用我还原了所有的变化。 https://github.com/laravel/framework/issues/2501

我找不到关于流明api资源集合的更多资料。任何人都可以看看,告诉我它是做错了什么。任何帮助将非常感激。

雄辩查询:

$companiesQuery = CpdAdmin::where('admin_id', $admin->admin_id)
->with(['companies' => function($query) use($request) {
    $query->where('air_id', $request->airport_id)
    ->where('status', '1')
    ->with(['companiesClosed' => function($query) use($request) {
        $query->where('close_status', 'active')
        ->where(function ($queryWhere) use($request) {
            $queryWhere->where('start_date', '>=', $request->start_date)
                ->where('start_date', '<=', $request->end_date)
                ->where(function ($queryType) {
                    $queryType->where('journey_type', 'Departure')
                        ->orWhere('journey_type', 'Both');
                });
        })
        ->orWhere(function ($queryWhere) use($request) {
            $queryWhere->where('end_date', '>=', $request->start_date)
                       ->where('end_date', '<=', $request->end_date)
                       ->where(function($queryType) {
                            $queryType->where('journey_type', 'Arrival')
                                      ->orWhere('journey_type', 'Both');
                        });
        });
    }]);
}])
->get();

JSON(在将其传递给资源集合之前):

json在将它传递给集合之前返回工作正常,我按照我期望的方式显示所有内容。

"data": [
    {
        "admin_id": 32,
        "admin_name": "Eden Parking",
        "email": "[email protected]",
        "admin_login": "[email protected]",
        "admin_information": "what information",
        "contact_number": 303633,
        "address": "paka dubai da",
        "invoice_email": "[email protected]",
        "contact_person": "uzair khan",
        "admin_rights": "1114",
        "admin_isactive": "1",
        "edit_status": 0,
        "created_at": null,
        "updated_at": null,
        "companies": [
            {
                "comp_id": 19,
                "comp_title": "S  Gatwick Meet & Greet",
                "comp_email": "[email protected]",
                "status": 1,
                "operation_data": "{\"operating_type\":\"24_7\",\"opening_status\":\"open\"}",
                "daily_bookings": 100,
                "monthly_bookings": 1000,
                "comp_admin": 32,
                "air_id": 14,
                "companies_closed": []
            },
            {
                "comp_id": 57,
                "comp_title": "Simply Parking Gatwick Meet & Greet",
                "comp_email": "[email protected]",
                "status": 1,
                "operation_data": "{\"operating_type\":\"24_7\",\"opening_status\":\"open\"}",
                "daily_bookings": 100,
                "monthly_bookings": 1000,
                "comp_admin": 32,
                "air_id": 14,
                "companies_closed": [
                    {
                        "id": 3101,
                        "start_date": "2019-03-04",
                        "end_date": "2019-03-15",
                        "journey_type": "Departure",
                        "close_status": "active",
                        "comp_id": 57,
                        "created_at": null,
                        "updated_at": null
                    },
                    {
                        "id": 3102,
                        "start_date": "2019-03-04",
                        "end_date": "2019-03-15",
                        "journey_type": "Both",
                        "close_status": "active",
                        "comp_id": 57,
                        "created_at": null,
                        "updated_at": null
                    }
                ]
            },
            {
                "comp_id": 149,
                "comp_title": "Park with us Gatwick",
                "comp_email": "[email protected]",
                "status": 1,
                "operation_data": null,
                "daily_bookings": 100,
                "monthly_bookings": 1000,
                "comp_admin": 32,
                "air_id": 14,
                "companies_closed": []
            }
        ]
    }
]

在这里,我将查询传递给资源集合

$companiesQueryResourceCollection = new CompanyResourceCollection($companiesQuery);

资源收集:

在我最后一个功能中

'companies_closed'=> $ eachCompany-> companies_closed

我可以循环companies_closed,但如果我能得到一些东西,如果我没有得到任何东西,只会抛出一个错误。

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class CompanyResourceCollection extends ResourceCollection
{
    public function toArray($request)
    {
        $response = $this->transformMultiple();
        return $response;
    }

    public function transformMultiple()
    {
        $transform = [];
        foreach ($this->collection as $item)
        {
            $transform[] = $this->transformSingle($item);
        }
        return $transform;
    }


    public function transformSingle($item)
    {
        return [
            'admin_id' => $item->admin_id,
            'admin_name' => $item->admin_name,
            'email' => $item->email,
            'contact_number' => $item->contact_number,
            'contact_person' => $item->contact_person,
            'companies' => $this->transformCompanies($item->companies),
        ];
    }

    public function transformCompanies($companies)
    {
        $transform = [];
        foreach ($companies as $singleCompany)
        {
            if( ($singleCompany->dailyBookingsCount < $singleCompany->daily_bookings) && ($singleCompany->monthlyBookingsCount < $singleCompany->monthly_bookings) )
            {
                // initially i was passing each company to its single resource but it was showing the same behaviour, so i moved the code to this file.
                // $transform[] = new CompanyResource($singleCompany);
                $transform[] = $this->transformEachCompany($singleCompany);
            }
        }
        return $transform;
    }

    public function transformEachCompany($eachCompany)
    {
        return [
            'comp_id' => $eachCompany->comp_id,
            'comp_title' => $eachCompany->comp_title,
            'comp_email' => $eachCompany->comp_email,
            'status' => $eachCompany->status,
            'operation_data' => json_decode($eachCompany->operation_data),
            'daily_bookings' => $eachCompany->daily_bookings,
            'monthly_bookings' => $eachCompany->monthly_bookings,
            'comp_admin' => $eachCompany->comp_admin,
            'air_id' => $eachCompany->air_id,
            'dailyBookingsCount' => $eachCompany->dailyBookingsCount,
            'monthlyBookingsCount' => $eachCompany->monthlyBookingsCount,

            // this returns 0 for all
            // 'companies_closed' => count($eachCompany->companies_closed)

            // this returns null for all
            // 'companies_closed' => $eachCompany->companies_closed

            // I can pass this array to another function but that would throw an error because the companies_closed is empty
            // 'companies_closed' => $eachCompany->companies_closed
        ];
    }
}
laravel lumen
1个回答
0
投票

好的,我自己修好了,这就是问题所在。

关系:

public function companiesClosed()
{
    return $this->hasMany(CpdCompanyClosed::class, 'comp_id', 'comp_id');
}

public function company()
{
    return $this->belongsTo(CpdCompany::class, 'comp_id');
}

请注意,关系命名约定是公司关闭

而在JSON回应中,我看到公司关闭了

我在API资源中使用companies_closed。当我改变它公司关闭它工作得很好。

原因:

关系命名约定是错误的。我将这一切都改为了这个命名约定company_closed。

© www.soinside.com 2019 - 2024. All rights reserved.