0

I am currently trying to set up an edit page where an order form is populated using json_decode to decode json information that was saved when the form was created. Because the form's size can change I have to create the correct number of inputs so that all the json data will have a place to be displayed. Fortunately as the inputs are numbered this should not be hard to do. Unfortunately I am not sure how to pick the last element of the json information that has been decoded. Currently I am using:

public function getEdit($id){
        $order = Order::where('id', '=', $id);

        if($order->count()) {
            $order                      = $order->first();
            $order->order_serialized    = json_decode($order->order_serialized);

            foreach($order->order_serialized as $key => $value){
                $order->$key = $value;
            }
            return View::make('orders.edit')
                    ->with('order', $order);
        }   else {
            return App::abort(404);
        }
    }

to decode the information and it is working splendidly but I need to be able to pick up the last element to be able to find the total number of inputs and am not sure how I could do this without disturbing the foreach loop. Any and all help would be greatly appreciated!! Thank you so much!

2 Answers 2

3

You can use the count and toArray methods to find the last item.

$nItem = $order->count();
$aOrder = $order->toArray();
$aLastItem = $aOrder[$nItem-1];
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! That sounds like its just what I was looking for.
Quick question - it says "Undefined offset: 6 " Any idea what this means?
are you sure that the indexes of the array are numbers and not column names? Undefined offset 6 means that it was looking for $aOrder[6] which doesn't exist
var_dump($aOrder) and see what the elements look like. Not sure it matters too much though, ceejayoz has a much cleaner answer and I had not realized there was a last() method.
Sorry I didn't see that there were comments on this. I dd($aOrder) and saw that it had all the inputs listed which is great. I looked at dd($nItem) and noticed it was 7 even though there were more items than that, so I'll have to look at that later. But thanks again!
3

Collections have a last() function to compliment the first() function.

4 Comments

I tried using last() like this foreach($order->order_serialized as $key => $value){ $order->$key = $value; if($order->$key->last()){ $last_element = $order->$key; } ; } but the debug said I was trying to use it on a non-object.
not in the foreach loop , outside of it, like $order->last() is the last row of your $order object
Sorry I didn't see your comment earlier. This looks great. I just have a quick question. In my database all of the form's input is stored in a "order_serialized" row in json_encoded info. To get to it I was thinking of using: $order->order_serialized = json_decode($order->order_serialized); $last_item = $order->order_serialized->last();
But that returns "Call to undefined method stdClass::last() ", any idea where I'm going wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.