如何在Laravel中使用RESTFUL API处理多个记录

时间:2019-05-03 09:38:34

标签: api laravel-5

请在下面找到代码。我已经使用foreach处理多个数据。但是,这给了我JSON响应错误。有帮助吗?

public function import(Request $request)
{   
    foreach ($request as $requests) {
        $pickup = new Pickup();

        if ($requests->status == 1) {
            $pickup->status = 'Published';
        } else {        //if 0 then unpublished
            $pickup->status = 'UnPublished';
        }

        if ($requests->fc_id != null && $requests->fc_id != 0) {
            $pickup->status = 'Accepted';
        }
        if ($requests->fc_id != null && $requests->fc_id != 0) {
            $pickup->agent_id = $requests->fc_id;
        } else {
            $pickup->agent_id = Null;
        }
        $pickup->customer_id = $requests->customer_id;
        $pickup->product_id = $requests->product_id;
        $pickup->price = $requests->price;
        $pickup->pickup_person = $requests->pickup_person;
        $pickup->pickup_email = $requests->pickup_email;
        $pickup->application_id = $requests->application_id;
        $pickup->home_address = $requests->home_address;
        $city = ucfirst(trim($requests->city));
        $city = ucfirst(strtolower($city));
        $pickup->city = $city;
        $pickup->state = $requests->state;
        $pickup->pincode = $requests->pincode;
        $pickup->office_address = $requests->office_address;
        $pickup->office_city = $requests->office_city;
        $pickup->office_state = $requests->office_state;
        $pickup->office_pincode = $requests->office_pincode;
        $pickup->mobile = $requests->mobile;
        $pickup->cheque_amt = $requests->cheque_amt;
        $pickup->loan_amt = $requests->loan_amt;
        $pickup->preferred_start_time = $requests->preferred_start_time;
        $pickup->preferred_end_time = $requests->preferred_end_time;
        $saved = $pickup->save();
        if ($saved) {
            return Response::json(array(
                        'error' => 0,
                        'message' => 'Data Inserted Successfully !'), 200
            );
        } else {
            return Response::json(array(
                        'error' => 1,
                        'message' => 'Data Not Inserted. Please try again !'), 201
            );
        }
    }
}

0 个答案:

没有答案