Adding a new record on a update field
up vote
0
down vote
favorite
I have a system where a user can edit their pets. On this page, they can also add another pet to their account.
This is what I used to edit the pet:
foreach ($request->pets as $pet) {
$clientPet = $client->pets()->find($pet)->first();
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders) {
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
How would I account for creating a new pet alongside this?
Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.
php laravel
add a comment |
up vote
0
down vote
favorite
I have a system where a user can edit their pets. On this page, they can also add another pet to their account.
This is what I used to edit the pet:
foreach ($request->pets as $pet) {
$clientPet = $client->pets()->find($pet)->first();
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders) {
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
How would I account for creating a new pet alongside this?
Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.
php laravel
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a system where a user can edit their pets. On this page, they can also add another pet to their account.
This is what I used to edit the pet:
foreach ($request->pets as $pet) {
$clientPet = $client->pets()->find($pet)->first();
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders) {
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
How would I account for creating a new pet alongside this?
Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.
php laravel
I have a system where a user can edit their pets. On this page, they can also add another pet to their account.
This is what I used to edit the pet:
foreach ($request->pets as $pet) {
$clientPet = $client->pets()->find($pet)->first();
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders) {
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
How would I account for creating a new pet alongside this?
Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.
php laravel
php laravel
edited 13 hours ago
asked 14 hours ago
Andre MacNamara
1609
1609
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can use findOrNew
, it will find the existing pet or instantiate a new instance.
docs:
foreach ($request->pets as $pet)
{
$clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders)
{
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use findOrNew
, it will find the existing pet or instantiate a new instance.
docs:
foreach ($request->pets as $pet)
{
$clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders)
{
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
|
show 3 more comments
up vote
0
down vote
You can use findOrNew
, it will find the existing pet or instantiate a new instance.
docs:
foreach ($request->pets as $pet)
{
$clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders)
{
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
You can use findOrNew
, it will find the existing pet or instantiate a new instance.
docs:
foreach ($request->pets as $pet)
{
$clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders)
{
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
You can use findOrNew
, it will find the existing pet or instantiate a new instance.
docs:
foreach ($request->pets as $pet)
{
$clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();
$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders)
{
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}
edited 13 hours ago
answered 13 hours ago
Remul
58410
58410
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
|
show 3 more comments
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
– Andre MacNamara
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
Are you passing the reminders with the request? At the moment your only getting them from the database.
– Remul
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
So I have to create new ones and update them? How do I do that
– Andre MacNamara
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
How are you passing the reminders in the request? can you update your question.
– Remul
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
That updated based on a relationship with the pet
– Andre MacNamara
13 hours ago
|
show 3 more comments
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265991%2fadding-a-new-record-on-a-update-field%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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