使用laravel 5从请求表更新用户表

时间:2017-02-28 05:03:13

标签: php arrays laravel

我想使用laravel 5从请求表更新用户表 请建议如何轻松完成。我有两个表数据,并希望根据请求表数据更新用户表,其中请求表有外国sm_id和用户表有主要ID请建议

提前致谢

请求数据:

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");
                            }

                          }

                        }
                    }       
            }
        }


      }
    }

注意:仅在预期结果中更新了phone,first_name和last_name

1 个答案:

答案 0 :(得分:1)

此代码将提供更新的数组以保存并指示是否有任何值已更改。只需要设置四个声明,然后是一个包含条件语句的foreach循环,该语句命令将数据推送到数组中。修改我的代码段以适合您的目的。

Demo

.gitignore

这是输出:

$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" => ""
);

$new_data=$user_data;
$user_id=$user_data["id"];  // declare target id
$request_ids=array_column($request_data,"sm_id");  // read sm_id column to array
$request_indexes=array_keys($request_ids,$user_id);  // declare matching indexes

// loop an subarrays that contain the appropriate sm_id
foreach(array_intersect_key($request_data,array_flip($request_indexes)) as $row){
    // if row contains one of the chosen values, overwrite the original data
    if(in_array($row["field_name"],array("phone","first_name","last_name"))){
        $new_data[$row["field_name"]]=$row["value"];
    }
}

if($user_data!=$new_data){
    echo "Changes to save";
}else{
    echo "No changes to save";
}
echo "<pre>";
var_export($new_data);
echo "</pre>";