0

我试图将资源支持添加到我的 laravel 8 应用程序中,但数据没有在我的资源中呈现,并且看起来没有调用资源方法。
我在 app/Library/Services/AdminCategoryCrud.php 文件中有:

<?php
namespace App\Library\Services;

use App\Models\Category;
use App\Http\Resources\Category as CategoryResource;  // I define Category Resource

class AdminCategoryCrud
{
    public function getFilteredListing($viewParamsArray, $requestData= [], $page= 1) {
        $backendItemsPerPage = 10;

        $filter_name = $requestData['filter_name'] ?? '';


        $totalCategoriesCount = Category::count();
        $startRowsFrom = (($page-1)*$backendItemsPerPage);;
        $categories = Category
            ::getByName($filter_name)
            ->withCount('adCategories')
            ->orderBy('categories.id', 'asc')
            ->get();

        $prevUrlLinks= getPaginationPrevUrlLinks($startRowsFrom, $backendItemsPerPage, $page);
        $nextUrlLinks= getPaginationNextUrlLinks($totalCategoriesCount, $categories->count(),  $backendItemsPerPage, $page);
        $viewParamsArray['page'] = $page;
        $viewParamsArray['prevUrlLinks'] = $prevUrlLinks;
        $viewParamsArray['nextUrlLinks'] = $nextUrlLinks;
        $viewParamsArray['categories'] = CategoryResource::collection($categories); // I call Category Resource
        $viewParamsArray['totalCategoriesCount'] = $totalCategoriesCount;
        return $viewParamsArray;
    }

}

资源定义app/Http/Resources/Category.php:

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\Category as CategoryModel;


class Category extends JsonResource
{
    public function toArray($request)
    {
        $imageFileData = [];// CategoryModel::readCategoryImageProps($this->id, $this->image, true);
        \Log::info(varDump($imageFileData, ' -1 JsonResource toArray $imageFileData::'));
        // Checking logs file I DO NOT see line above
        return [
            'id'              => $this->id,
            'name'            => $this->name,
            'slug'            => $this->slug,
            'description'     => $this->description,
            'content_shortly' => $this->content_shortly,

            'image'            => $this->image,
            'categoryImgProps' => $imageFileData ?? null,

            'created_at'           => $this->created_at,
            'created_at_formatted' => getCFFormattedDateTime($this->created_at),
            'updated_at'           => $this->updated_at,
            'updated_at_formatted' => getCFFormattedDateTime($this->updated_at),
        ];
    }
}

所以在结果数据中我看不到字段:'categoryImgProps'、'created_at_formatted'、'updated_at_formatted',我希望在我的视图中看到结果数据。

修改的 :

我检查了我之前使用资源的应用程序之一,例如:

public function index()
{
    $filter_title = $this->requestData['filter_title'] ?? '';
    $filter_published = $this->requestData['filter_published'] ?? '';
    $filter_is_homepage = $this->requestData['filter_is_homepage'] ?? '';
    $pages = Page
        ::getByTitle($filter_title)
        ->getByIsHomepage($filter_is_homepage)
        ->getByPublished($filter_published)
        ->with('creator')
        ->get();

    return $this->sendResponse(PageResource::collection($pages), '');

}

那不是刀片应用程序,而是rest api应用程序和

PageResource::collection($pages) 

工作正常,文件 app/Http/Resources/Page.php 有内容:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\Page As PageModel;
use App\Http\Resources\User as UserResource;


class Page extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $imageFileData = PageModel::readPageImageProps($this->id, $this->image, true);
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'content' => $this->content,
            'content_shortly' => $this->content_shortly,

            'creator_id' => $this->creator_id,
            'creator' => new UserResource($this->whenLoaded('creator')),

            'is_homepage' => $this->is_homepage,
            'is_homepage_label' => PageModel::getIsHomepageLabel($this->is_homepage),

            'published' => $this->published,
            'published_label' => PageModel::getPublishedLabel($this->published),

            'image' => $this->image,
            'imageFileData' => $imageFileData ?? null,

            'meta_description' => $this->meta_description,
            'meta_keywords' => $this->meta_keywords,

            'created_at' => $this->created_at,
            'created_at_formatted' => getCFFormattedDateTime($this->created_at),
            'updated_at' => $this->updated_at,
            'updated_at_formatted' => getCFFormattedDateTime($this->updated_at),
        ];
    }
}

但在我的情况下,函数退出时没有调用 CategoryResource::collection,而是为刀片模板设置数组......这可能是我的问题的原因吗?对我来说很奇怪...

4

0 回答 0