I have a table like given below and this table is going to be used for project images.
id: primary key
project_id: a column that has correlation with projects.
order_num: represents image list order for specific project (eg. interior view of project, exterior view of construction project, a different aspect etc...)
+---------+-------------+-----------+
| id | project_id | order_num |
+---------+-------------+-----------+
| 1 | 15 | 0 |
+---------+-------------+-----------+
| 2 | 15 | 0 |
+---------+-------------+-----------+
| 3 | 16 | 0 |
+---------+-------------+-----------+
| 4 | 16 | 0 |
+---------+-------------+-----------+
| 5 | 16 | 0 |
+---------+-------------+-----------+
.
.
.
GOES ON LIKE THIS
What i want to do:
+---------+-------------+-----------+
| id | project_id | order_num |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 |
+---------+-------------+-----------+
| 4 | 16 | 2 |
+---------+-------------+-----------+
| 5 | 16 | 3 |
+---------+-------------+-----------+
I use Slim with Eloquent
I figured out something like this but i got bad method error for save() method.
$find = Images::where('project_id', '=', $project->id)->count();
$img = Images::where('project_id', '=', $project->id);
for ($i = 1; $i <= $find; $i++) {
$img->order_num = $i;
$img->save();
}
Am i doing something wrong? Any help would be very appreciated.
UPDATE [!!] Both answers are give the correct results but according to the odan's information, apokryfos's answer is clean code. Thanks for your all helps.
try simply this
$img
seems to be a query builder object.If you do
$img = Images::where('project_id', '=', $project->id)->get();
you will get everything that needs updading example:This will update all image indices for a single project id. If you want to do all of them then you can do: