如何将多个数据存储和编辑到一个JSON列中?

时间:2019-01-11 11:47:29

标签: mysql json laravel laravel-blade jsonb

我有一个属于User模型的Model Profile。配置文件模型具有一个“信息” JSON列,该列存储许多数据,例如个人信息,工作信息,联系信息等,这是JSON字段的结构,但是在工作字段中,将有多个数据,例如多次工作经历,当前职位等

到目前为止,我只能存储一个工作数据,并显示在刀片​​文件中。但是我需要存储多个工作信息。

这是JSON字段的样子:

async  requestLocationPermission(){
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        'title': 'Example App',
        'message': 'Example App access to your location '
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the location")
      alert("You can use the location");
    } else {
      console.log("location permission denied")
      alert("Location permission denied");
    }
  } catch (err) {
    console.warn(err)
  }
}

 async componentDidMount() {
   await requestLocationPermission()
  }

这是我在控制器中存储数据的方式:

 {"work": {"info": "work", "company": "Augnitive", "working_to": null,
    "designation": "Software Engineer", "working_from": "2019-01-14", 
    "responsibilities": null}, "contact": {"info": "contact", "email": 
    "ki.tushar21@gmail.com", "mobile": "01681654863", "address": "House 156, 
    Sultangonj, Rayer Bazar Dhaka 1209, West Agargaon, West Agargaon", 
    "facebook": "fb.com", "linkedin": "linkedin.com", "citizenship": 
    "Bangladesh"}, "personal": {"bday": "2019-01-07", "info": "personal", 
    "blood": "A(+VE)", "gender": "Male"}, "education": {"info": "education", 
    "edu_type": "SSC", "institute": "svf", "graduation": "2010"}}

从我的Blade文件中,我传递了隐藏的输入字段以跟踪数据来自哪种形式,例如:

protected $info_mapping = [
    'work' => 'work',
    'education' => 'education',
    'contact' => 'contact',
    'personal' => 'personal'

];

public function store(Request $request)
{
    $type = $request->get('info');
    $name = $request->get('name');
    if (isset($name)) {
        $user = Auth::user();
        $user->name = $request->get('name');
        $user->student_id = $request->get('student_id');
        $user->save();
    }

    if (!isset($this->info_mapping[$type])) {
        return response()->json([
            'error' => true,
            'message' => "Type is invalid",
        ]);
    }
    $form_data = $request->except('name', '_token', 'student_id');
    $profile = auth()->user()->profile;
    $data = [];
    if (isset($profile->information)) {
        $data = $profile->information;
    }
    $data[$this->info_mapping[$type]] = $form_data;
    $profile->information = $data;
    $profile->save();

    return redirect()->back();
}

0 个答案:

没有答案