使用 REST API 处理多响应数据

Posted

技术标签:

【中文标题】使用 REST API 处理多响应数据【英文标题】:Handle Multiple Response Data with REST API 【发布时间】:2019-09-24 01:08:27 【问题描述】:

我在处理 json 多数据时遇到问题。我没有得到多个数据,如果我使用“数组”,那么它也会在数据库中创建具有空值的单个记录。

保存数据的后端代码

 
       $pickup = new PickupData();

       $pickup->pickup_person = $request->pickup_person;

       $pickup->office_city = $request->office_city;

       $pickup->office_state = $request->office_state;

       $pickup->office_pincode = $request->office_pincode;

       $pickup->pickup_email = $request->pickup_email;

       $pickup->preferred_start_time = $request->preferred_start_time;

       $pickup->preferred_end_time = $request->preferred_end_time;

       $pickup->mobile = $request->mobile;

       $pickup_date = Carbon::parse($request->pickup_date)->format('d-m-Y');

       $pickup->pickup_date = $pickup_date;

       $saved = $pickup->save();

我需要类似的响应,它应该保存到数据库中

[
  
    "pickup_person":"SanPune6",
    "city":"pune",
    "office_address":"viman nagar",
    "office_city":"Pune",
    "office_state":"MH",
    "office_pincode":"121212",
    "pickup_email":"office@gmail.com",
    "preferred_start_time":"12:38:29",
    "preferred_end_time":"13:02:33",
    "mobile":"9090909090",
    "pickup_date":"12-04-2019"
  ,
  
    "pickup_person":"SanPune6",
    "city":"pune",
    "office_address":"viman nagar",
    "office_city":"Pune",
    "office_state":"MH",
    "office_pincode":"121212",
    "pickup_email":"office@gmail.com",
    "preferred_start_time":"12:38:29",
    "preferred_end_time":"13:02:33",
    "mobile":"9090909090",
    "pickup_date":"12-04-2019"
  
]

【问题讨论】:

【参考方案1】:
    像这样创建资源
  php artisan make:resource PickupResourceCollection
    创建另一个类似的资源
  php artisan make:resource PickupResource
    在 PickupController 的 index 方法中这样做
use use App\Http\Resources\PickupResourceCollection;
  ...
  public function index()
  
     $pickups = Pickup::all();
     return response()->json(new PickupResourceCollection($pickups),200);
  
 ...
    在 PickupResourceCollection 中这样做
....
 public function toArray($request)
 
     return [
         'data' => $this->collection->transform(function ($pickups) 
             return new PickupResource($pickups);
         )
     ];
 
 ....
    在 PickupResource 中这样做
....
 public function toArray($request)
 
    return [
       "pickup_person"       =>$this->pickup_person,
       "city"                =>$this->city,
       "office_address"      =>$this->office_address,
       "office_city"         =>$this->office_city,
       "office_state"        =>$this->office_state,
       "office_pincode"      =>$this->office_pincode,
       "pickup_email"        =>$this->pickup_email,
       "preferred_start_time"=>$this->preferred_start_time,
       "preferred_end_time"  =>$this->preferred_end_time,
       "mobile"              =>$this->mobile,
       "pickup_date"         =>"$this->pickup_date
   ];
 
 ....

就是这样:)

【讨论】:

感谢@Bimal,但我想要类似 [ "application_id": "1" , "application_id": "2" 我已按照上述所有步骤操作,但无法将多条记录保存在数据库中。为什么?你能解释一下吗 另外,我响应它显示了数据库表中可用的所有数据 @shree 将数据保存在表中使用 PickupData::create($request->all()),然后使用上述步骤检索数据,上述步骤将返回数据在 [ "application_id": "1" , "application_id": "2" ] 格式 谢谢。我使用相同的功能来存储数据。但是,我需要一次存储多个数据。另外,想以 [ "application_id": "1" , "application_id": "2" ] 格式传递数据。【参考方案2】:
$pickupDetails =          [
                             
                                 "pickup_person":"SanPune6",
                                 "city":"pune",
                                 "office_address":"viman nagar",
                                 "office_city":"Pune",
                                 "office_state":"MH",
                                 "office_pincode":"121212",
                                 "pickup_email":"office@gmail.com",
                                 "preferred_start_time":"12:38:29",
                                 "preferred_end_time":"13:02:33",
                                 "mobile":"9090909090",
                                 "pickup_date":"12-04-2019"
                             ,
                             
                                 "pickup_person":"SanPune6",
                                 "city":"pune",
                                 "office_address":"viman nagar",
                                 "office_city":"Pune",
                                 "office_state":"MH",
                                 "office_pincode":"121212",
                                 "pickup_email":"office@gmail.com",
                                 "preferred_start_time":"12:38:29",
                                 "preferred_end_time":"13:02:33",
                                 "mobile":"9090909090",
                                 "pickup_date":"12-04-2019"
                             
                         ];
         $pickupDetails = $request->all();     
         foreach($pickupDetails as $pickup) 
             Pickup::create($pickup);  
         
         return response()->json("success",200); 

如果它可以帮助你支持我:)

【讨论】:

【参考方案3】:

是的,您可以保存为数组。

$pickup = PickupData::create($request->all()) // to save all request data
$pickup = PickupData::create($request->except(['var1', 'var2'])) // to save all request data except var1 & var2

$pickup 将只返回新插入的行。

【讨论】:

以上是关于使用 REST API 处理多响应数据的主要内容,如果未能解决你的问题,请参考以下文章

如何将firebase REST API响应转换为打字稿中的数组?

收到响应后,是不是可以选择删除 REST API(RestAssured) 中请求中设置的多部分内容?

处理跨域 REST 响应的 301 和位置标头

错误响应上的 REST-API 不同的内容类型

无法在 SpringBoot 中使用 ControllerAdvice 返回 rest api 响应

iOS:如何从 REST SwiftUi 访问和使用 API 响应数据