如何在Laravel中验证和存储对象列表

时间:2017-11-12 05:38:33

标签: android laravel phpstorm retrofit

我对Laravel服务器进行了Retrofit API调用:

@POST("/dualsimlog")
Call<ResponseBody> submitLogs(@Body List<ServerLog> serverLogs);

以下是Retrofit实施:

List<ServerLog> unsubmitted = logHelper.getLogInfo();
retrofit2.Call<ResponseBody> call = logServices.submitLogs(unsubmitted);

                    call.enqueue(new retrofit2.Callback<ResponseBody>() {
                        @Override
                        public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                            L.m(response.message());
                        }

                        @Override
                        public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
                            L.m("Submission failed.");
                        }
                    });

单个对象的My Laravel商店API是:

public function store(Request $request)
    {
        // validating object variables
        $rules = [
            'device_id' => 'required|string',
            'type' => 'required|string',
            'sim1_cost' => 'required|numeric|min:0',
            'sim2_cost' => 'required|numeric|min:0',
            'cost_saved' => 'required|numeric|min:0',
            'call_duration' => 'required|numeric|min:0',
            'created_at' => 'required|date',
        ];

        $data = $request->all();
        $validator = Validator::make($data, $rules);

        if ($validator->fails()) {
            return $this->errorResponse("Validation error.",400 );
        }
        $product = DualSimLog::create($data);

        return $this->showMessage("Successful.");
    }

如何修改API以验证和插入ServerLog对象列表?

此外,在使用Android发送API请求时,如何在PhpStorm中调试Laravel项目?

1 个答案:

答案 0 :(得分:0)

您需要在改造中使用@FormUrlEncoded,请参阅此link