mercredi 1 février 2017

Laravel5: How do I handle API post requests for an array of json objects?

Using Laravel 5.3 as backend API, I need to handle post requests for a json object and an array of objects.

For an object:

order = {table_id:1, food_id: 2};

I did the following.

public function postOrder(Request $request) {
    $order = new Order();
    $order->table_id  = $request->input('table_id');
    $order->food_id   = $request->input('food_id');
    $order->save();

    return response()->json(['order' => $order], 201);
}

And for an array of objects,

orders = [{table_id:1, food_id:2}, {table_id:3,food_id:4}];

I want to try:

public function postOrders(Request $request)
{
    foreach ($request as $anOrder)
    {
        $order = new Order();
        $order->table_id  = $anOrder->input('table_id');
        $order->food_id   = $anOrder->input('food_id');
        $order->save();
    }

    return response()->json(???, 201);
}

But that gives me an error while trying to post:

Call to undefined method Symfony\Component\HttpFoundation\ParameterBag::input()

How do I handle this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kpSMYs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire