i have codes about create and update at same time in my Controller file, when i create a new data it should also update a json array column in my User Table
$rumah = Rumah::create($validatedData);
$updateData = [
'lihatAlamat' => json_encode($rumah->id),
'editAlamat' => json_encode($rumah->id)
];
auth()->user()->update($updateData);
return redirect('/rumah');
So the problem with this when i create a data, it doesn’t add array data but overwrite whole array inside lihatAlamat and editAlamat column
Example problem like this:
lihatAlamat = ["1","2"]
editAlamat = ["1","2"]
when i create data it becomes:
lihatAlamat = 3
editAlamat = 3
how to add new id number inside array
3
Answers
well, i found my own answer
You can use this instead:
Hope this solve your problem.
Hi are you trying to update these two (lihatAlamat ,editAlamat) columns values or you want to add old values + new values to combine.
if i have to to this is would use belongsToMany relationship with pivot table and used sync or async with values so it would be easy to use.
But as of now you can use this
In model u can use mutator and accesors
Then in controller