HasMany relation(or any relation) data inserted but not return on the same time | Laravel | Eloquent Model2019 Community Moderator ElectionBulk Insertion in Laravel using eloquent ORMAdd a custom attribute to a Laravel / Eloquent model on load?Get the Last Inserted Id Using Laravel EloquentLaravel Check If Related Model ExistsLaravel eloquent add extra models to hasManyLaravel eloquent hasmany->hasmanyFind child model hasMany relation in laravel eloquentLaravel Eloquent hasMany filtered by parent tableLaravel hasManyThrough manipulates user_idlaravel eloquent hasmany or hasmany trough
Plagiarism of code by other PhD student
Misplaced tyre lever - alternatives?
School performs periodic password audits. Is my password compromised?
Is divide-by-zero a security vulnerability?
How can I handle a player who pre-plans arguments about my rulings on RAW?
How do we objectively assess if a dialogue sounds unnatural or cringy?
PTIJ: What dummy is the Gemara referring to?
When was drinking water recognized as crucial in marathon running?
An Undercover Army
“I had a flat in the centre of town, but I didn’t like living there, so …”
Specific Chinese carabiner QA?
Can I solder 12/2 Romex to extend wire 5 ft?
Rationale to prefer local variables over instance variables?
GDAL GetGeoTransform Documentation -- Is there an oversight, or what am I misunderstanding?
Formatting a table to look nice
Where is the fallacy here?
3.5% Interest Student Loan or use all of my savings on Tuition?
How to mitigate "bandwagon attacking" from players?
is 'sed' thread safe
Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?
How can I highlight parts in a screenshot
Why did the Cray-1 have 8 parity bits per word?
I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?
Why are special aircraft used for the carriers in the United States Navy?
HasMany relation(or any relation) data inserted but not return on the same time | Laravel | Eloquent Model
2019 Community Moderator ElectionBulk Insertion in Laravel using eloquent ORMAdd a custom attribute to a Laravel / Eloquent model on load?Get the Last Inserted Id Using Laravel EloquentLaravel Check If Related Model ExistsLaravel eloquent add extra models to hasManyLaravel eloquent hasmany->hasmanyFind child model hasMany relation in laravel eloquentLaravel Eloquent hasMany filtered by parent tableLaravel hasManyThrough manipulates user_idlaravel eloquent hasmany or hasmany trough
I have a laravel application in which the setting_types and user's settings are saved into different models.
User.php:
/*
* Getting the user's notification setting.
*/
public function notificationSetting()
return $this->hasMany('AppNotificationSetting');
/*
* Controller function to get the user's settings
*/
public function getSetting(Request $request)
$userSetting = $user->notificationSetting;
// check new settings are inserted for user or not.
if (someCondition)
// add new settings for user.
$user->notificationSetting()->save(new NotificationSetting(['user_id' => $user_id, "notification_type_id" => 121]));
print_r($user->notificationSetting); // still rec. Old values.
return $user->notificationSetting;
As you can see that I insert the relation object but I didn't receive on the same time. and if I hit again (this time my someCondition become false) so it will return the update records.
laravel laravel-5 eloquent
add a comment |
I have a laravel application in which the setting_types and user's settings are saved into different models.
User.php:
/*
* Getting the user's notification setting.
*/
public function notificationSetting()
return $this->hasMany('AppNotificationSetting');
/*
* Controller function to get the user's settings
*/
public function getSetting(Request $request)
$userSetting = $user->notificationSetting;
// check new settings are inserted for user or not.
if (someCondition)
// add new settings for user.
$user->notificationSetting()->save(new NotificationSetting(['user_id' => $user_id, "notification_type_id" => 121]));
print_r($user->notificationSetting); // still rec. Old values.
return $user->notificationSetting;
As you can see that I insert the relation object but I didn't receive on the same time. and if I hit again (this time my someCondition become false) so it will return the update records.
laravel laravel-5 eloquent
add a comment |
I have a laravel application in which the setting_types and user's settings are saved into different models.
User.php:
/*
* Getting the user's notification setting.
*/
public function notificationSetting()
return $this->hasMany('AppNotificationSetting');
/*
* Controller function to get the user's settings
*/
public function getSetting(Request $request)
$userSetting = $user->notificationSetting;
// check new settings are inserted for user or not.
if (someCondition)
// add new settings for user.
$user->notificationSetting()->save(new NotificationSetting(['user_id' => $user_id, "notification_type_id" => 121]));
print_r($user->notificationSetting); // still rec. Old values.
return $user->notificationSetting;
As you can see that I insert the relation object but I didn't receive on the same time. and if I hit again (this time my someCondition become false) so it will return the update records.
laravel laravel-5 eloquent
I have a laravel application in which the setting_types and user's settings are saved into different models.
User.php:
/*
* Getting the user's notification setting.
*/
public function notificationSetting()
return $this->hasMany('AppNotificationSetting');
/*
* Controller function to get the user's settings
*/
public function getSetting(Request $request)
$userSetting = $user->notificationSetting;
// check new settings are inserted for user or not.
if (someCondition)
// add new settings for user.
$user->notificationSetting()->save(new NotificationSetting(['user_id' => $user_id, "notification_type_id" => 121]));
print_r($user->notificationSetting); // still rec. Old values.
return $user->notificationSetting;
As you can see that I insert the relation object but I didn't receive on the same time. and if I hit again (this time my someCondition become false) so it will return the update records.
laravel laravel-5 eloquent
laravel laravel-5 eloquent
edited 19 hours ago
Inzamam Idrees
610218
610218
asked 20 hours ago
sachin kumarsachin kumar
4001315
4001315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Since the save()
method returns a boolean, you could write it like this:
$user->notificationSetting()
->save(
$notificationSetting = new NotificationSetting([
'user_id' => $user_id,
'notification_type_id' => 121
])
);
return $notificationSetting;
You might also be able to use the create()
method instead, that will return the instance of the model, but only if the attributes are fillable of course.
If you want to retrieve all the related records of a model at any time, you can use the load()
method like this:
$user->load('notificationSetting');
It is also important the use the plural form for a hasMany
relation in order to distinguish it from a hasOne
or a belongsTo
relation:
public function notificationSettings()
return $this->hasMany('AppNotificationSetting');
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use theload()
method (see updated answer). Also it is important to use the plural formnotificationSettings
instead of the singular.
– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
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%2f55021567%2fhasmany-relationor-any-relation-data-inserted-but-not-return-on-the-same-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since the save()
method returns a boolean, you could write it like this:
$user->notificationSetting()
->save(
$notificationSetting = new NotificationSetting([
'user_id' => $user_id,
'notification_type_id' => 121
])
);
return $notificationSetting;
You might also be able to use the create()
method instead, that will return the instance of the model, but only if the attributes are fillable of course.
If you want to retrieve all the related records of a model at any time, you can use the load()
method like this:
$user->load('notificationSetting');
It is also important the use the plural form for a hasMany
relation in order to distinguish it from a hasOne
or a belongsTo
relation:
public function notificationSettings()
return $this->hasMany('AppNotificationSetting');
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use theload()
method (see updated answer). Also it is important to use the plural formnotificationSettings
instead of the singular.
– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
add a comment |
Since the save()
method returns a boolean, you could write it like this:
$user->notificationSetting()
->save(
$notificationSetting = new NotificationSetting([
'user_id' => $user_id,
'notification_type_id' => 121
])
);
return $notificationSetting;
You might also be able to use the create()
method instead, that will return the instance of the model, but only if the attributes are fillable of course.
If you want to retrieve all the related records of a model at any time, you can use the load()
method like this:
$user->load('notificationSetting');
It is also important the use the plural form for a hasMany
relation in order to distinguish it from a hasOne
or a belongsTo
relation:
public function notificationSettings()
return $this->hasMany('AppNotificationSetting');
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use theload()
method (see updated answer). Also it is important to use the plural formnotificationSettings
instead of the singular.
– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
add a comment |
Since the save()
method returns a boolean, you could write it like this:
$user->notificationSetting()
->save(
$notificationSetting = new NotificationSetting([
'user_id' => $user_id,
'notification_type_id' => 121
])
);
return $notificationSetting;
You might also be able to use the create()
method instead, that will return the instance of the model, but only if the attributes are fillable of course.
If you want to retrieve all the related records of a model at any time, you can use the load()
method like this:
$user->load('notificationSetting');
It is also important the use the plural form for a hasMany
relation in order to distinguish it from a hasOne
or a belongsTo
relation:
public function notificationSettings()
return $this->hasMany('AppNotificationSetting');
Since the save()
method returns a boolean, you could write it like this:
$user->notificationSetting()
->save(
$notificationSetting = new NotificationSetting([
'user_id' => $user_id,
'notification_type_id' => 121
])
);
return $notificationSetting;
You might also be able to use the create()
method instead, that will return the instance of the model, but only if the attributes are fillable of course.
If you want to retrieve all the related records of a model at any time, you can use the load()
method like this:
$user->load('notificationSetting');
It is also important the use the plural form for a hasMany
relation in order to distinguish it from a hasOne
or a belongsTo
relation:
public function notificationSettings()
return $this->hasMany('AppNotificationSetting');
edited 19 hours ago
answered 19 hours ago
piscatorpiscator
2,91921023
2,91921023
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use theload()
method (see updated answer). Also it is important to use the plural formnotificationSettings
instead of the singular.
– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
add a comment |
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use theload()
method (see updated answer). Also it is important to use the plural formnotificationSettings
instead of the singular.
– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
I try both methods create and save. The data is added into the table successfully but I can't able to get updated records after insertion.
– sachin kumar
19 hours ago
It that case you can use the
load()
method (see updated answer). Also it is important to use the plural form notificationSettings
instead of the singular.– piscator
19 hours ago
It that case you can use the
load()
method (see updated answer). Also it is important to use the plural form notificationSettings
instead of the singular.– piscator
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
Yes, you are right. Thanx.
– sachin kumar
19 hours ago
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%2f55021567%2fhasmany-relationor-any-relation-data-inserted-but-not-return-on-the-same-time%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