我有以下表格
我想在任何人调用单个类别内容时显示通过变换器过滤的内容。
我的控制器 CategoryController.php
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$category = Category::find($id)->contentsWithRelationship()->paginate(10);
if (!$category) {
return $this->respondNotFound('Category does not exist.');
}
return $this->respond(['data' => $this->categoryTransformer->transformCollection($category->all())]);
}
型号: Category.php
/**
* Get Cotents from the category.
*
* @return void
*/
public function contents()
{
return $this->belongsToMany('App\Models\Content');
}
/**
* Get Contents from the category.
*
* @return void
*/
public function contentsWithRelationship()
{
return $this->contents('App\Models\Content')->with('tags', 'images');
}
变压器接口: Transformer.php
abstract class Transformer
{
/**
* Transform a collection
*
* @param $item
* @return array
*/
public function transformCollection($item)
{
return array_map([$this, 'transform'], $item);
}
/**
* Transform items
*
* @param $item
* @return mixed
*/
abstract public function transform($item);
}
和我的类别变换器如下: CategoryTransformer.php
class CategoryTransformer extends Transformer
{
/**
* Transformer Category.
*
* @param $category
* @return array
*/
public function transform($category)
{
$images_array = array_map([$this, 'transformImages'], $category['images']->toArray());
$tags_array = array_map([$this, 'transformTags'], $category['tags']->toArray());
return [
'title' => $category['title'],
'description' => $category['description'],
'image' => $category['image'],
'status' => $category['status'],
'images'=> $images_array,
'tags' => $tags_array,
];
}
/**
* this method will transform the image.
*
* @param array $image
* @return void
*/
public function transformImages($image)
{
return [
'id' => (int) $image['id'],
'title' => $image['title'],
'description' => $image['description'],
];
}
/**
* This method will transform the tags.
*
* @param [type] $image
* @return void
*/
public function transformTags($tag)
{
return [
'id' => (int) $tag['id'],
'title' => $tag['title'],
];
}
}
我找到了关系转换的解决方案,但我无法在此处应用它。任何人都可以帮我解决。
如何归档我的结果?
当我使用上面解释的代码时,我获得10次单记录。
下面的示例json
。
{
"data": [
{
"title": "sint",
"description": "Vitae totam qui voluptatem consequatur sapiente id. Nesciunt eveniet accusamus laborum eum a. Natus autem assumenda omnis voluptate omnis. At consequatur est non rerum.",
"image": null,
"status": "Active",
"images": [
{
"id": 4,
"title": "doloremque.png",
"description": "Dolor rem vitae et omnis enim autem ut. Veritatis eius fugit consequatur quo occaecati facere. Ea natus labore voluptatem aut. Id sit qui dolorem ipsum non qui vel."
},
{
"id": 137,
"title": "enim.png",
"description": "Soluta nihil quas corrupti eaque illo. Non non beatae placeat amet voluptas inventore."
},
{
"id": 240,
"title": "repellendus.png",
"description": "Recusandae at dolores modi et iure nulla. Maxime qui officiis ratione qui delectus ab quia facere. Et est ipsa unde illo qui assumenda inventore. Blanditiis quo numquam odio ipsam veniam cum."
}
],
"tags": [
{
"id": 1,
"title": "est"
},
{
"id": 19,
"title": "qui"
},
{
"id": 42,
"title": "fuga"
},
{
"id": 27,
"title": "mollitia"
},
{
"id": 45,
"title": "perferendis"
},
{
"id": 23,
"title": "dolor"
},
{
"id": 19,
"title": "qui"
}
]
},
另外,如何使用指定类别的内容为图像和标签创建转换。我不想删除 分页并希望安排转换,我得到了我需要但需要帮助来完成转换课程。 如何添加打击代码它也给我错误的数组
$images_array = array_map([$this, 'transformImages'], $category['images']->toArray());
$tags_array = array_map([$this, 'transformTags'], $category['tags']->toArray());
请帮我解决这个问题。