0

So I'm sending from Ionic app to API via POST request an array of condiments which are on mobile part represented as checkboxes. I want to get the checkboxes which are checked so I can create table rows accordingly.

The method that is called upon route triggering has this part:

$condiments = Input::only('condiments');

foreach ($condiments as $condiment) {
    if ($condiment['checked'] == 1) {
        OrderCondiment::create([
            'order_item_id' => $orderItem, 
            'condiment_id' => $condiment->id
        ]);
    }
}

But I get Illegal string offset 'checked' error. I tried with $condiment->checked but then I get Trying to get the property of non-object error...what am I missing...besides coffee

EDIT

dd($condiments)

array:1 [
  "condiments" => "[{"id":1,"name":"ut","price":3,"created_at":null,"updated_at":null,"checked":1},{"id":2,"name":"ipsam","price":5,"created_at":null,"updated_at":null,"checked":1},{"id":3,"name":"dolores","price":10,"created_at":null,"updated_at":null,"checked":1},{"id":4,"name":"esse","price":3,"created_at":null,"updated_at":null,"checked":0},{"id":5,"name":"aliquid","price":1,"created_at":null,"updated_at":null,"checked":0},{"id":6,"name":"sunt","price":3,"created_at":null,"updated_at":null,"checked":0},{"id":7,"name":"saepe","price":1,"created_at":null,"updated_at":null,"checked":0},{"id":8,"name":"impedit","price":10,"created_at":null,"updated_at":null,"checked":0},{"id":9,"name":"dolores","price":4,"created_at":null,"updated_at":null,"checked":0},{"id":10,"name":"veniam","price":2,"created_at":null,"updated_at":null,"checked":0}]"
]
7
  • What does dd('$condiments) show? Commented Apr 29, 2016 at 12:33
  • Anything from Input is an array; you cannot do this $condiment->id it should be: $condiment['id'] Commented Apr 29, 2016 at 12:37
  • Yes, but it is erroring on the row where "if" clause is Commented Apr 29, 2016 at 12:39
  • Ok, well, @AlexeyMezenin is right in that you should be using dd to do some debugging. Commented Apr 29, 2016 at 12:41
  • 1
    Dude...I am trying to work it out for the last 2hrs, I didn't come here after 5mins of being stuck... Commented Apr 29, 2016 at 12:46

1 Answer 1

8

convert string to array

$condiments = Input::only('condiments');
$condiments= json_decode( $condiments['condiments']) ;
foreach ($condiments as $condiment) {
    if ($condiment->checked == 1) {
        OrderCondiment::create(['order_item_id' => $orderItem, 
                                'condiment_id' => $condiment->id]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Cannot use object of type stdClass as array -> error on line 32 (where "if" clause is)
use $condiment->checked instead $condiment['checked']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.