Eloquent save() method not working inside for loop
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.
php laravel eloquent slim
|
show 1 more comment
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.
php laravel eloquent slim
is itorder_num
ororder_id
you need to change?
– apokryfos
Nov 25 '18 at 17:52
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38
|
show 1 more comment
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.
php laravel eloquent slim
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.
php laravel eloquent slim
php laravel eloquent slim
edited Nov 26 '18 at 8:51
bgul
asked Nov 25 '18 at 17:51
bgulbgul
144
144
is itorder_num
ororder_id
you need to change?
– apokryfos
Nov 25 '18 at 17:52
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38
|
show 1 more comment
is itorder_num
ororder_id
you need to change?
– apokryfos
Nov 25 '18 at 17:52
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38
is it
order_num
or order_id
you need to change?– apokryfos
Nov 25 '18 at 17:52
is it
order_num
or order_id
you need to change?– apokryfos
Nov 25 '18 at 17:52
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38
|
show 1 more comment
2 Answers
2
active
oldest
votes
$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:
$index = 1;
Images::where('project_id', '=', $project->id)->get()
->each(function ($img) use (&$index) {
$img->order_num = $index++;
$img->save();
});
This will update all image indices for a single project id. If you want to do all of them then you can do:
$index = 0;
$lastProjectId = null;
Images::orderBy('project_id')->get()
->each(function ($img) use (&$index, $lastProjectId) {
if ($lastProjectId == null) {
$lastProjectId = $img->project_id;
}
if ($img->project_id != $lastProjectId) {
$index = 1;
$lastProjectId = $img->project_id;
}
$img->order_num = $index++;
$img->save();
});
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
add a comment |
try simply this
$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
$img->order_id = $i + 1;
$img->save();
}
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.
– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470245%2feloquent-save-method-not-working-inside-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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:
$index = 1;
Images::where('project_id', '=', $project->id)->get()
->each(function ($img) use (&$index) {
$img->order_num = $index++;
$img->save();
});
This will update all image indices for a single project id. If you want to do all of them then you can do:
$index = 0;
$lastProjectId = null;
Images::orderBy('project_id')->get()
->each(function ($img) use (&$index, $lastProjectId) {
if ($lastProjectId == null) {
$lastProjectId = $img->project_id;
}
if ($img->project_id != $lastProjectId) {
$index = 1;
$lastProjectId = $img->project_id;
}
$img->order_num = $index++;
$img->save();
});
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
add a comment |
$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:
$index = 1;
Images::where('project_id', '=', $project->id)->get()
->each(function ($img) use (&$index) {
$img->order_num = $index++;
$img->save();
});
This will update all image indices for a single project id. If you want to do all of them then you can do:
$index = 0;
$lastProjectId = null;
Images::orderBy('project_id')->get()
->each(function ($img) use (&$index, $lastProjectId) {
if ($lastProjectId == null) {
$lastProjectId = $img->project_id;
}
if ($img->project_id != $lastProjectId) {
$index = 1;
$lastProjectId = $img->project_id;
}
$img->order_num = $index++;
$img->save();
});
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
add a comment |
$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:
$index = 1;
Images::where('project_id', '=', $project->id)->get()
->each(function ($img) use (&$index) {
$img->order_num = $index++;
$img->save();
});
This will update all image indices for a single project id. If you want to do all of them then you can do:
$index = 0;
$lastProjectId = null;
Images::orderBy('project_id')->get()
->each(function ($img) use (&$index, $lastProjectId) {
if ($lastProjectId == null) {
$lastProjectId = $img->project_id;
}
if ($img->project_id != $lastProjectId) {
$index = 1;
$lastProjectId = $img->project_id;
}
$img->order_num = $index++;
$img->save();
});
$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:
$index = 1;
Images::where('project_id', '=', $project->id)->get()
->each(function ($img) use (&$index) {
$img->order_num = $index++;
$img->save();
});
This will update all image indices for a single project id. If you want to do all of them then you can do:
$index = 0;
$lastProjectId = null;
Images::orderBy('project_id')->get()
->each(function ($img) use (&$index, $lastProjectId) {
if ($lastProjectId == null) {
$lastProjectId = $img->project_id;
}
if ($img->project_id != $lastProjectId) {
$index = 1;
$lastProjectId = $img->project_id;
}
$img->order_num = $index++;
$img->save();
});
answered Nov 25 '18 at 18:00
apokryfosapokryfos
18.6k43056
18.6k43056
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
add a comment |
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
Thanks for your helps @apokryfos. I haven't heard the each() method(I have to do some research about this). Thanks for your additional code for upgrade all values
– bgul
Nov 25 '18 at 18:27
add a comment |
try simply this
$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
$img->order_id = $i + 1;
$img->save();
}
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.
– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
add a comment |
try simply this
$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
$img->order_id = $i + 1;
$img->save();
}
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.
– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
add a comment |
try simply this
$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
$img->order_id = $i + 1;
$img->save();
}
try simply this
$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
$img->order_id = $i + 1;
$img->save();
}
answered Nov 25 '18 at 17:54
simonecoscisimonecosci
81158
81158
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.
– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
add a comment |
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.
– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
Thank you very much for your answer. I think the problem is caused by for loop... But this one works perfectly. Thanks again.
– bgul
Nov 25 '18 at 18:01
you may need to do
$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.– apokryfos
Nov 25 '18 at 18:01
you may need to do
$img = Images::where('project_id', '=', $project->id)->get()->values()
. There's a chance the collection result is keyed by the id.– apokryfos
Nov 25 '18 at 18:01
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
It works well without values(). But i'll keep that in mind thanks for your sharing knowledge. :)
– bgul
Nov 25 '18 at 18:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470245%2feloquent-save-method-not-working-inside-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
is it
order_num
ororder_id
you need to change?– apokryfos
Nov 25 '18 at 17:52
ahh sorry for writing wrong but problem is not there
– bgul
Nov 25 '18 at 17:54
Please use the query builder and not this Active Record Antipattern.
– odan
Nov 25 '18 at 18:47
@odan hi, odan, you mean the each() function given below?
– bgul
Nov 26 '18 at 8:36
@bgul Yes, in this context, updating multiple records in a loop is a real antipattern.
– odan
Nov 26 '18 at 8:38