1

I have to following array from inputs

array:9 [
  "columns" => "4"
  "device_id" => array:4 [
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "4"
  ]
  "hub_id" => "11"
  "usage" => array:4 [
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "4"
  ]
   ....

In my foreach loop i get back only one value not all

$devices = $request->all();    
foreach ($devices["device_id"] as $device) {
           dd($device);
}

This will return only 1 one value not all.

I have problem displaying all the value and saving them to database. Witch would be the fast and the right way ?

5
  • Array looks ok to me. Commented Aug 23, 2018 at 8:32
  • Witch would be the best way to save the data from the array ? Commented Aug 23, 2018 at 8:32
  • Please show your foreach loop and the code that saves this data to the database Commented Aug 23, 2018 at 8:33
  • Check my updated question, i want to loop all at once but if i loop them all at once i get only the array number. Commented Aug 23, 2018 at 8:35
  • @Edo You're using dd function. dd means die and dump. So the program will exit in the first iteration. Try to do echo $device; or print_r($device); Commented Aug 23, 2018 at 9:01

2 Answers 2

1

The array and the behaviour you are getting is normal.

$devices = $request->all();    
foreach ($devices["device_id"] as $device) {
           dd($device);
}

This will indeed return 1, because your cursor is at the first value of $devices["device_id"]. If you wait for the next iteration, it'll be 2, then 3 and 4.

Remember you can also write your foreach this way:

foreach ($devices["device_id"] as $index => $device)

, where $index will be equal to the index related to the current value.

If you want all values, you can just simply dd($devices["device_id"]), it'll return you this array:

array:4 [
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "4"
 ]
Sign up to request clarification or add additional context in comments.

4 Comments

What you want to say is the following: If i try to save dd($device); to eg. $all_devices_id would store all values in the database ? Or?
dd($something) is just for debugging purpose, if you could update your question with the related model and database table, i could show you how to use this array and save these data into the table more accurately
Please show me how could i save array data to database, from what i understood if i do the following this would save all data from this table: foreach ($devices_id as $device_id) { $device_id = $device_id; } $device = new Device(); $device->device_id = $device_id; $device->save();
I would have all values for the array correct ? Even when i dd i see only one ?
1

You do not save array in a database. Databases are not supposed to save multidimensional structures, since they do not support them.

You must convert your array into a format that the database is able to save, for example JSON or PHP serialize. It is actually very common to save data in this way if you do not need to search it easily at a later time.

Nevertheless, I would use a setter in your model to achieve this:

public function setAttributeNameAttribute($values)
{
    $this->attributes['attribute_name'] = json_encode($values);
}

public function getAttributeNameAttribute($values)
{
    return json_decode($values);
}

2 Comments

Okay, how do i loop the array then ?
You just loop it once you have access to the model, since Laravel magic getters will convert the JSON string to an object automatically.

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.