0

I have multiple checkboxes on a form generated from a model to view which is presented this way:

{{Form::open(array('action'=>'LaboratoryController@store'))}}
@foreach (Accounts::where('accountclass',$i)->get() as $accounttypes)
{{ Form::checkbox('accounttype[]', $accounttypes->id)}}
@endforeach
{{Form::submit('Save')}}
{{Form::close()}}

When I return the Input::all() from my controller store method, it outputs like this:

{"client":"1","accounttype":["2","3","5","12","13","14","16","31","32","33"]}

Now I want to store the accounttypes array values to the accounts table by looping through the array in order to store each values on each rows using the same client id.

The same accounttype will be inserted to the second table but with different data.

So, my accounts table:

+-------------+---------------------------+------+-----+---------+----------------+
| Field       | Type                      | Null | Key | Default | Extra          |
+-------------+---------------------------+------+-----+---------+----------------+
| accountno   | int(11) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| accounttype | int(11)                   | NO   |     | NULL    |                |
| client      | int(11)                   | NO   |     | NULL    |                |
| created_at  | datetime                  | NO   |     | NULL    |                |
+-------------+---------------------------+------+-----+---------+----------------+

My controller store method:

public function store()
{

    $accounttypes = Input::get('accounttype');

    if(is_array($accounttypes)) 
    {
        for($i=0;$i < count($accounttypes);$i++)
        {
            // insert data on first table (accounts table)
            $accountno = DB::table('accounts')->insertGetId(array('client'=>Input::get('client'),'accounttype',$accounttypes[$i]));

            // insert data on the second table (account summary table) using the account no above
            // DB::table('accountsummary')...blah blah
        }
    }

    return Redirect::to('some/path');
}

The function seems to work but only for the first array value which is "2". I don't know what's wrong with the code but it seems that the loop doesn't go through the rest of the values. I was testing other loop methods like while and foreach but still the looping variable ($i) returns zero.

I was thinking if laravel controller doesn't allow loops on POST methods.

Your inputs are greatly appreciated. Thanks..

2
  • Comment out the code in your for statement and just add return (or echo) $accounttypes[$i]; What happens then? Commented Oct 14, 2014 at 20:23
  • When I just do return $accounttypes[$i];, it just returns "2" (without the quotes).. When I manually put a number like return $accounttypes[5];, it actually returns the correct value which is "14". The loop doesn't go through, it only returns the first accounttype array value. Commented Oct 15, 2014 at 1:22

1 Answer 1

2

Foreach and DB::insert() works for me.

foreach ($accounttypes as $accounttype) {
        DB::insert('INSERT INTO tb_accounts (accounttype,client) VALUES (?,?)', array($accounttype,Input::get('client'));
    }

I just need to create separate query to get the last insert id because DB::insertGetId doesn't work the way I want it. But that's another issue. Anyway, thanks.

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

Comments

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.