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

jhyaps's avatar

How to update only one column in database ?

Hello all,

i am creating a custom menu with 1 level depth concept as per the requirement

currently i am sorting my menu as per the menu_order

alt text

at store method, i am checking menu_order with the MenuItem present in there and adding +1 in it, also i havenot added any form option to the user to add new menu_order

$data['menu_order'] = MenuItem::where('menu_id', $data['menu_id'])->count() + 1;

But in update method, i want to change as per the user wants, suppose if he want his order {uncategorized to be 1) which is currently in menu_order 4, as per the given image

i want to change its menu_order to 1 along with 2 in about -us, 3 in simple post , 4 in our research, and so on

how to sync only one column in the table as per the user request ???

can any one help me ???

0 likes
4 replies
Tray2's avatar
UPDATE table
SET column = <value>
WHERE id = <id>
1 like
Snapey's avatar

Assuming you reorder the menu items according to the order that the user entered;

$submenus = [23,10,12,5];

foreach($submenus as $key => $submenu_id) {
	Menu::where('id',$submenu_id)->update(['order' => $key);
}

The right solution for you depends on how you collect the desired order from the user interface

3 likes
Udev's avatar

Is this for a cms? Not sure I understand the question but are you are trying to order the items and persist to the db? you can add a button to move up/down the order. on move up, get the item + next item(if it isn't the last) and switch the menu_id by subtracting from next and adding to the one moving down. on move down, get the item + previous item(if it isn't the first) and also switch the order.

1 like
amitsolanki24_'s avatar

You need write update query with where condition that has unique field or column value

Please or to participate in this conversation.