2

I am trying to save data that is returned in array form to my table. However, I need to access the string in both GET inputs to do so. How can I achieve that? I can do it with one GET but not both.

    public function postDisableDates() {

    $input = Input::all();  

    foreach($input['date'] as $date) {

        $db_date = strtotime($date);

        $dateFormat = date('Y-m-d', strtotime("-1 month", $db_date));

    DB::table('bk_disable_date')
            ->delete();

    DB::table('bk_disable_date')
            ->insert(array(
                'date' => $dateFormat,
                'office' => $input['office']
            ));                     
    }       

}

var dump $input

array(3) { ["_token"]=> string(40) "Dr0pu8RUbTTe1t058Fb3sDjKQLLk1KMBnBrpV7m6" ["office"]=> array(4) { [0]=> string(3) "Office1" [1]=> string(10) "Office2" [2]=> string(10) "Office3" [3]=> string(10) "Office4" } ["date"]=> array(4) { [0]=> string(17) "25 December, 2013" [1]=> string(17) "14 December, 2013" [2]=> string(17) "05 December, 2013" [3]=> string(17) "23 November, 2013" } } 

var dump of $dateFormat in foreach loop

string(10) "2013-11-25" string(10) "2013-11-14" string(10) "2013-11-05" string(10) "2013-10-23" 

The $dateFormat part seems to be ok. I just need the office part to work the same way. Probably an easier way to achieve this.

my input names are set up as office[] and date[]. If that helps.

The disable date table is as follows

id  date    office
30  2013-11-25  Office1
31  2013-11-14  Office2
32  2013-11-05  Office3
33  2013-10-23  Office4
2
  • So your values are not sent over to you as key value pairs within objects or arrays and therefore are just consecutive, such as form submission, date[]=2013/11/10&office[]=somewhere&date[]=2013/11/11&office[]=elsewhere and so on and so forth? Commented Nov 11, 2013 at 1:01
  • Looks like they are being sent via array: array(3) { ["_token"]=> string(40) "Dr0pu8RUbTTe1t058Fb3sDjKQLLk1KMBnBrpV7m6" ["office"]=> array(4) { [0]=> string(3) "Office1" [1]=> string(10) "Office2" [2]=> string(10) "Office3" [3]=> string(10) "Office4" } ["date"]=> array(4) { [0]=> string(17) "25 December, 2013" [1]=> string(17) "14 December, 2013" [2]=> string(17) "05 December, 2013" [3]=> string(17) "23 November, 2013" } } Commented Nov 11, 2013 at 1:25

1 Answer 1

3

If the office and date keys are the same (which I assume), you could try that:

public function postDisableDates() {

  $input = Input::all();  

  DB::table('bk_disable_date')
    ->delete();

  foreach($input['date'] as $key => $date) {

    $db_date = strtotime($date);
    $dateFormat = date('Y-m-d', strtotime("-1 month", $db_date));

    DB::table('bk_disable_date')
      ->insert(array(
        'date' => $dateFormat,
        'office' => $input['office'][$key]
      )
    );                     
  }       
}

Hope that helps

Sign up to request clarification or add additional context in comments.

6 Comments

That's a start but it's only saving the first row. But it does save correctly.
@Lynx: I only see one row in your example, don't I?
No, sorry I must not have explained it correctly. It's a row of data. so office would have 'Office1', 'Office2'..etc...and dates go along with those offices. I will edit my question to show how the data is saving.
See edit for format of table to get an idea of what i am trying to achieve
@Lynx: I removed the delete from the foreach. That does not seem right the delete the table everytime before inserting the new row. If you want to have a fresh start, the delete should be before the foreach.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.