簡體   English   中英

在 Laravel 中,如何從我的 API Controller 內的*表*返回特定的*列*? 我正在使用 Laravel 5

[英]In Laravel, how can i return a specific *column* from a *table* inside my API Controller ? I'm using Laravel 5

問題

如何從表中返回特定[在 API 控制器中]? 我使用了pluck ,但它從 output 中刪除了列名。 我還需要包含列名。 我引用的 $product 表(在下面的代碼中引用)只是一個包含名稱、價格、折扣率等產品內容的表。

簡要描述;簡介

呈現 API 響應樣本 -

{
   "data" : [
      "Graham",
      "Marina Philip",
      "David Doomer",
           ],
   "message" : "",
   "success" : true
}

預期反應 -

[
  {
    "name": "Graham",
  },
  {
    "name": "Marina Philip",
  },  {
    "name": "David Doomer",
  },

]

API 來自 APIController 的路由:

Route::resource('searchlist', 'API\SearchlistAPIController');

索引 function from my SearchlistAPIController.php [具體功能]

           public function index(Request $request)
            {
                try{
                    $this->productRepository->pushCriteria(new RequestCriteria($request));
                    $this->productRepository->pushCriteria(new LimitOffsetCriteria($request));
                    $this->productRepository->pushCriteria(new ProductsOfFieldsCriteria($request));
                    if($request->get('trending',null) == 'week'){
                        $this->productRepository->pushCriteria(new TrendingWeekCriteria($request));
                    }
                    else{
                        $this->productRepository->pushCriteria(new NearCriteria($request));
                    }

                    $products = $this->productRepository->all();
        
                } catch (RepositoryException $e) {
                    return $this->sendError($e->getMessage());
                }
    //Here i've got the value of the table $Product with a bunch of columns from my database..
        
                $sendinger = $products->pluck('name');    
 
   //I'm trying to filter the columns send here. But i lost the column name as well.            
                
               return $this->sendResponse($sendinger->toArray(),'');
            }

我的sendResponse方法的內容:

public function sendResponse($result, $message) { return Response::json(ResponseUtil::makeResponse($message, $result)); }

另外,我怎樣才能從我的 Json 響應中刪除它?

 "message" : "",
   "success" : true

在我的腦海中,我相信$products->pluck('name')->toArray()將創建返回一個索引數組。 例如[ "Graham", "Marina Philip", "David Doomer" ]

你能做的是...

$sendinger = $products->pluck('name')->map(function($name) {
    return [ 'name' => $name ];
});
                
return $this->sendResponse($sendinger->toArray(),'');

更多關於映射的信息: https://laravel.com/docs/7.x/collections#method-map

未經測試,但應該引導您朝着正確的方向前進。


對於您的其他問題(從響應中刪除“成功”和“消息”)...

看來您正在使用Laravel Generator ResponseUtil class 來添加附加參數。 在此處查看源代碼: https://github.com/InfyOmLabs/laravel-generator/blob/7.0/src/Utils/ResponseUtil.php

只需替換最后一行:

return $this->sendResponse($sendinger->toArray(),'');

return response()->json( $sendinger->toArray() );

這應該可以解決問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM