Laravel API资源未返回带条件的正确值

时间:2019-12-07 11:06:33

标签: php laravel

我是Laravel API的新手,我想返回一本推荐=== 1的书

在我的资源中,我有这个

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'about' => $this->about,
        'content' => $this->content,
      //  'image' => asset('/storage/'.$this->image),
      'image' => $this->image_url,
       // 'recommended' => $this->recommended,
        'recommended' => $this->when($this->recommended === 1, $this->recommended),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'author' => $this->author,
   ];

我想在推荐=== 1时归还书

我的桌子是这样

public function up()
{
    Schema::create('books', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('about');
        $table->string('image');  
        $table->string('image_url');
        $table->string('epub_url');
        $table->integer('author_id'); 
        $table->string('publisher');  
        $table->year('year');
        $table->boolean('recommended')->default(0);
        $table->timestamps();    
    });

使用此功能,我能够在网络上实现相同的目标

public function index()
{
    $data = array();
    $data['recommends'] = Book::where('recommended', 1)->take(10)->get();
    $data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
   return view('welcome', compact("data"));
}

但是我不知道如何使用Laravel API复制相同内容。

更新

我可以使用this

在网络上实现相同的目的
public function index()
{
    $data = array();
    $data['recommends'] = Book::where('recommended', 1)->take(10)->get();
    $data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
   return view('welcome', compact("data"));
}

但是我不知道如何使用Laravel API复制相同内容。

通常我会使用API​​资源获取所有类似的书籍或帖子

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'about' => $this->about,
        'content' => $this->content,
        'image' => $this->image_url,
        'recommended' => $this->recommended,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'author' => $this->author,
   ];

并在我的控制器中这样称呼它

public function indexapi()
{
  return BookResource::collection(Book::with('author')->Paginate(16));
}

但是有些情况下recommended == 1,有些情况recommended == 0,在这种情况下,我只想在推荐== 1时才返回数据

我知道我的问题很令人困惑

谢谢。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果我做对了,您只想过滤并获取(建议属性== 1)的图书。如果是这种情况,则不应在您的收藏夹文件中执行此操作。您应先在Controller中执行此过滤过程,然后再将任何数据传递给Collection。

这是我的一个项目中的一些代码示例。

在ProductController.php文件中

public function index()
{
    return new ProductCollection( Product::where('recommended','1')->get() );
}

如您所见,我正在过滤产品以仅获得推荐的产品。然后,我将此过滤后的数据发送到ProductCollection。这样,集合只会返回我想要的数据。

在ProductCollection.php文件中

public function toArray($request)
{
    return [
        'data' => $this->collection->map( function($data) {
            return [
                'id' => (integer) $data->id,
                'name' => $data->name,
                'category_id' => $data->category_id,
                'brand_id' => $data->brand_id,
                'photos' => json_decode($data->photos),
                'gtin' => $data->gtin
            ];
        })
    ];
}

我不必在“收藏夹”中进行任何更改。因为以这种方式,Collection应该为它获取的每个数据完成这项工作。