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

virgiltu's avatar

Insert array of in database

I have a table named videos->watched , i am trying to insert the auth user id as an array in this column.

the ides is simple the videos will be watched the controller will add to the array a new id, when the video gets updated the entire column gets dumped and it starts over.

Any ideas how i could insert the array? Sadly eloquent seems to think that $array is part of the query and I get an error.

Error: Call to a member function update() on integer

Array: [{"0":"","user":1}]

Code: $array = array(array_add([$watched],'user' ,Auth::user()->id)); DB::table('videos')->whereId($id)->increment('views')- >update(['watched' => $array]);

0 likes
4 replies
zachleigh's avatar

What kind of database are you using? You cant actually store arrays in mysql.
There are probably lots of ways to do this, but I think I would have a simple 'watched' table with 'user_id' and 'video_id' columns. Set up relationships on the table so that you can easily go between videos, users, and watched tables. When a user watches a video, a new row is added with their user_id and the watched video_id. To find the videos a certain user simply search by user name.

bobbybouwmann's avatar

You only need to pass a array with values to the update function

$watched = [
    'user' => Auth::user()->id,
];

DB::table('videos')->yourQueryStuff()->update($watched);
virgiltu's avatar

thank you,

i was trying to avoid having to request a new table every time i send this request, i am going to look if maybe comma delimited will be the way to go. having to create a new table and search through all of them again seems overkill when i can just swift through the data i already have requested. unfortunately i am paying per request on this projects server so this will add a lot of cost for an extra request in the long term. but thank you for your response, it i much appreciated it.

virgiltu's avatar

I found a solution using just PHP unfortunately Laravel does not have these built in, using the serialize() and unserialize() functions.

Thanks for the help.

Please or to participate in this conversation.