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










0















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.










share|improve this question




























    0















    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.










    share|improve this question


























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 19 hours ago









      Inzamam Idrees

      610218




      610218










      asked 20 hours ago









      sachin kumarsachin kumar

      4001315




      4001315






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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');






          share|improve this answer

























          • 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












          • Yes, you are right. Thanx.

            – sachin kumar
            19 hours ago










          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
          );



          );













          draft saved

          draft discarded


















          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









          0














          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');






          share|improve this answer

























          • 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












          • Yes, you are right. Thanx.

            – sachin kumar
            19 hours ago















          0














          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');






          share|improve this answer

























          • 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












          • Yes, you are right. Thanx.

            – sachin kumar
            19 hours ago













          0












          0








          0







          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');






          share|improve this answer















          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');







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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

















          • 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












          • 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



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived