Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ufodisko's avatar

Save, update and retrieve array to and from the database

How do you save, update and retrieve an array from and to the database? I have a skills table with 3 fields: id, user_id, skill_name

At the moment, when I select skills from a chosen js multi-select field, it gets inserted in the database and it looks like this

a:2:{i:0;s:3:"php";i:1;s:5:"mysql";}

However, if I went back and selected new skills including skills I have already selected, a new record in the table will be created with the new skills along side the skills I've selected earlier. My table will look something like this, as you can see mysql is present twice.

Screenshot of the table

Instead, I need it to update the table row of the user instead of creating a new row with the same values.

How may I achieve that? Also, am I doing this thing correctly? Is that how you're supposed to save an array to the database?

App\User

public function skill() {
        return $this->hasMany('App\Skill');
    }

App\Skill

public function user() {
        return $this->belongsTo('App\User');
    }

UserController

$skills = $request->input('skills');
$json_skills = serialize($skills);

if($skills) {
            $skill = new Skill;
            $skill->user_id = Auth::user()->id;
            $skill->skill_name = $json_skills;
            $user->skill()->save($skill);
}
0 likes
14 replies
Snapey's avatar
$skill = new Skill;

says create a new skill.

You have made life hard by saving json string and not saving the actual skill.

Anyway, whatever, have a look at findOrCreate to get the previous skill for the user and overwrite it.

ufodisko's avatar

Oh, I see what I did.

$skill = new Skill;

This will create a new table row no matter what.

But saving $skills directly gives an error

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

How do you suggest I do this?

Snapey's avatar

can't see any preg_replace in your code? What line errors?

ufodisko's avatar

From the error page, I can see this

at HasOneOrMany->save(object(Skill)) in UserController.php line 78

which is this line

$user->skill()->save($skill);

ufodisko's avatar

I am still getting the same error and in the "skill_name" column in the database, the word "Array" is being saved.

ufodisko's avatar

There you go, and thank you so much.

public function saveSettings(Requests\SettingsUpdateRequest $request, Skill $skill) {
        $user = User::find(Auth::user()->id);

        $user->age = $request->input('age');
        $profile_picture = $request->file('profile_image_url');
        $profile_banner = $request->file('profile_banner_url');

        if($profile_picture) {
            $profile_picture_filename = str_random(8) . '-' . $user->id . '.' . $profile_picture->getClientOriginalExtension();
            Storage::disk('local')->put($profile_picture_filename, File::get($profile_picture));
            $user->profile_image_url = $profile_picture_filename;
        }
        if($profile_banner) {
            $profile_banner_filename = str_random(8) . '-' . $user->id . '.' . $profile_banner->getClientOriginalExtension();
            Storage::disk('local')->put($profile_banner_filename, File::get($profile_banner));
            $user->profile_banner_url = $profile_banner_filename;
        }

        $skills = $request->input('skills');
        $json_skills = serialize($skills);

        if($skills) {
            //$skill = new Skill;
            $skill->user_id = Auth::user()->id;
            $skill->skill_name = $skills;
            $skill->save();
        }

        $user->save();

        flash('Your profile has been updated.', 'success');

        return redirect()->back();
    }
Snapey's avatar

Is this function meant to work when editing?

The problem is that you now are not initialising $skill at all. I said the wrong method earlier, you want to use the old skill for the user, or create a new skill.

   if($skills) {
            $skill = Skill::firstOrNew(['user_id',$user->id]);
            $skill->skill_name = $skills;
            $skill->save();
        }

I notice you are very inconsistent about the use of $user. There is no need to query the database for it at the start. just use $user=Auth::user(); and then just $user from then on.

ufodisko's avatar

Yes, the function works apart from the skills.

I got a new error this time, and it doesn't make any sense since I don't have columns in the db named 0 and 1

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from users_skills where (0 = user_id and 1 = 7) limit 1)

Snapey's avatar

Why is it trying to access a table users_skills, sounds like a pivot

1 like
ufodisko's avatar

You're right. I just realized that I don't need a separate table to store the skills because I'm using an array.

So I added a new column called skills and started saving using $user but I still got the same preg_match() error as before if I pass it as a normal array, but it works if I pass it as json.

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

$skills = $request->input('skills');
        $skills_json = serialize($skills);

        if($skills) {
            $user->skills = $skills;
        }

$user->save();
1 like
jlrdw's avatar

This is yet another interesting post. And yes.

Please or to participate in this conversation.