流明5.6.4 API资源收集未返回所有数据

时间:2019-03-04 20:47:59

标签: laravel lumen

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

我在Laravel 5.6.39中使用了相同的方法(通过函数束传递数据),在Laravel中工作得很好。但是当我在流明5.6.4中执行此操作时,它不起作用。

我认为这可能是缓存问题,因此我几乎遵循此url中的所有方法,但未成功。由于它不起作用,我还原了所有更改。 https://github.com/laravel/framework/issues/2501

我在lumen 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": "edenparking@hotmail.com",
        "admin_login": "edenparking@hotmail.com",
        "admin_information": "what information",
        "contact_number": 303633,
        "address": "paka dubai da",
        "invoice_email": "invoice@gmail.com",
        "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": "mcpgatwick@gmail.com",
                "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": "mcpgatwick@gmail.com",
                "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": "mcpgatwick@gmail.com",
                "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,但是如果我能得到一些东西,如果我什么都没得到,foreach只会抛出错误。

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

1 个答案:

答案 0 :(得分:0)

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

关系:

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

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

请注意,关系命名约定为 companiesClosed

在JSON响应中我看到的是 companies_closed

我在API资源中使用了 companies_closed 。当我将其更改为 companyClosed 时,它的效果很好。

原因:

关系命名约定错误。我将其全部更改为该命名约定company_closed。