Monday, February 27, 2017

map array value from two array and update the table laravel

I have two array what i want get all information from request based on column 'field_name' and 'value' and compare with users fields and matched value should updated users table based on column match 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!! Chef profile updated successfully");
       }
      }
     }
    }
   }
  }
 }
}

Request has the array
Array ( 

     [0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => Sa [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] => Ak [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] => Kumar [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => ) )

Here is users array
Array (

 [id] => 4 [social_id] => [role_id] => 0 [name] => [first_name] => mn [last_name] => jk [gender] => male [dob] => 02-02-2017 [language] => english [location] => London [address] => ABCD [email] => ma.ja@sxyz.com [phone] => 7894561230 [provider] => website [biography] => Here is about  [created_at] => 2017-02-22 12:16:56 [updated_at] => 2017-02-22 12:16:56 [deleted_at] => )



from Latest question asked on Laravel tag.


via Developer Account

Advertisement