Monday, February 27, 2017

update the user table from request table using laravel 5

I want to update the user table from request table using laravel 5 Please suggest how it can be done with ease. I have two tables data and want to update user table based on request table data where request table has foreign sm_id and user table has primary id please suggest

Thanks in advance

Request data:

Array (

 [0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => G2 [created_at] => 2017-02-27 14:17:35 [updated_at] => 2017-02-24 11:05:03 [deleted_at] => )

 [1] => Array ( [id] => 2 [sm_id] => 4 [field_name] => phone [value] => 123467890 [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => )

 [2] => Array ( [id] => 3 [sm_id] => 4 [field_name] => first_name [value] => John [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => )

 [3] => Array ( [id] => 4 [sm_id] => 4 [field_name] => last_name [value] => Hunny [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => ) )

User Data:

Array
(
    [id] => 4
    [social_id] => 
    [role_id] => 0
    [name] => 
    [first_name] => max
    [last_name] => rony
    [gender] => male
    [dob] => 02-02-2017
    [language] => english
    [location] => USA
    [address] => ABCD
    [email] => max.jrny@jioc.com
    [phone] => 7894561230
    [id_proof] => 
    [id_proof_path] => 
    [remember_token] => 
    [provider] => website
    [biography] => Here is about 
    [approve] => 1
    [created_at] => 2017-02-22 12:16:56
    [updated_at] => 2017-02-22 12:16:56
    [deleted_at] => 
)


Expected result in user table

Array
(
    [id] => 4
    [social_id] => 
    [role_id] => 0
    [name] => 
    [first_name] => john
    [last_name] => Hunny
    [gender] => male
    [dob] => 02-02-2017
    [language] => english
    [location] => USA
    [address] => ABCD
    [email] => max.jrny@jioc.com
    [phone] => 123467890
    [id_proof] => 
    [id_proof_path] => 
    [remember_token] => 
    [provider] => website
    [biography] => Here is about 
    [approve] => 1
    [created_at] => 2017-02-22 12:16:56
    [updated_at] => 2017-02-22 12:16:56
    [deleted_at] => 
)


Here is my code

 public function verifyInformation($id){

      if($id){

        $getRequest = request::all()->toArray();

        $user = User::where('id', $id)->first()->toArray();

        foreach($getRequest as $row=>$value){
            foreach($value as $rowKey => $result){
                   $array_key = array_keys($user);

                    $arrayDiff = array_diff($value, $user);
                    $find = $arrayDiff['field_name'];  


                    if(in_array($find, $array_key)){
                        foreach($array_key as $k => $v){

                          if($v == $find){
                            $fieldKey = $v;
                            $fieldValue =  $arrayDiff['value'];

                            if($fieldValue){

                              $user = new User;  
                              $user->$fieldKey = $fieldValue;
                              $user->save();

                              return redirect()->back()->with("status", "Well done!! profile updated successfully");
                            }

                          }

                        }
                    }       
            }
        }


      }
    }

Note: only phone, first_name and last_name updated in expected result




via Manish Jakhmola

Advertisement