Home:ALL Converter>How to update multiple rows and database with same value at once? Laravel

How to update multiple rows and database with same value at once? Laravel

Ask Time:2013-07-17T07:15:34         Author:Aleksa

Json Formatter

I'm stuck in code. I managed to create a form that adds a multiple number of rows in db but I do not know how to update them at the same time when i need to use product_code as id.

This is table 1 (products) structure:

id | product_name | product_code
1  | Name1        | 101
2  | Name2        | 102
3  | Name3        | 103
4  | Name4        | 104

This is table 2 (products_ing) structure:

id | product_code | ing_name  | ing_weight
1  | 101          | INGName1  | 110
2  | 101          | INGName2  | 10
3  | 101          | INGName3  | 54
4  | 101          | INGName4  | 248

This is edit function:

    public function post_edit($id = null)
{

    if(Input::get('submit'))
    {
        // ID
        if($id !== null)
        {
            $id = trim(filter_var($id, FILTER_SANITIZE_NUMBER_INT));
        }


        $input = Input::all();

        $validation = Validator::make($input);

        else
        {
            foreach ($input as $key => $value)
            {
                $input[$key] = ($value !== '') ? trim(filter_var($value, FILTER_SANITIZE_STRING)) : null;
            }

            try
            {

                DB::table('products')
                    ->whereIn($id)
                    ->update(array(
            'product_name'          => $items['product_name'],
            'product_code'          => $items['product_code']
                ));


            }

    }

    return $this->get_edit($id);
}

With this I can only edit *product_name* and product_code from products table by id. But I'm trying to update multiple rows from db with same product_code that would serve as id.

// I found on Google images one good image that explains what I'm trying to do but with Laravel:

IMAGE LINK

Is there a solution? Thank you in advance!

Author:Aleksa,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17688533/how-to-update-multiple-rows-and-database-with-same-value-at-once-laravel
yy