Illuminate/Eloquent: how to use belongsTo in constructor










0














i would like to provide data from the "foreign key table" in the current model.



I am currently achieving this by a function (getBrand).



But now I would like to have this accessable as a property. So I added a property ($brand) and wanted to fill it by calling the function (getBrand) in the constructor of the model.



class Car extends Eloquent 
public $brand;

public function __construct()
parent::__construct();
$this->brand = getBrand();


public function getBrand()
return $this->belongsTo('AppBrand', 'FK_BrandId')->first()->Brandname;




But this creates an error during the execution of the constructor:



Trying to get property of non-object


Any ideas how to solve this? Thanks!










share|improve this question




























    0














    i would like to provide data from the "foreign key table" in the current model.



    I am currently achieving this by a function (getBrand).



    But now I would like to have this accessable as a property. So I added a property ($brand) and wanted to fill it by calling the function (getBrand) in the constructor of the model.



    class Car extends Eloquent 
    public $brand;

    public function __construct()
    parent::__construct();
    $this->brand = getBrand();


    public function getBrand()
    return $this->belongsTo('AppBrand', 'FK_BrandId')->first()->Brandname;




    But this creates an error during the execution of the constructor:



    Trying to get property of non-object


    Any ideas how to solve this? Thanks!










    share|improve this question


























      0












      0








      0







      i would like to provide data from the "foreign key table" in the current model.



      I am currently achieving this by a function (getBrand).



      But now I would like to have this accessable as a property. So I added a property ($brand) and wanted to fill it by calling the function (getBrand) in the constructor of the model.



      class Car extends Eloquent 
      public $brand;

      public function __construct()
      parent::__construct();
      $this->brand = getBrand();


      public function getBrand()
      return $this->belongsTo('AppBrand', 'FK_BrandId')->first()->Brandname;




      But this creates an error during the execution of the constructor:



      Trying to get property of non-object


      Any ideas how to solve this? Thanks!










      share|improve this question















      i would like to provide data from the "foreign key table" in the current model.



      I am currently achieving this by a function (getBrand).



      But now I would like to have this accessable as a property. So I added a property ($brand) and wanted to fill it by calling the function (getBrand) in the constructor of the model.



      class Car extends Eloquent 
      public $brand;

      public function __construct()
      parent::__construct();
      $this->brand = getBrand();


      public function getBrand()
      return $this->belongsTo('AppBrand', 'FK_BrandId')->first()->Brandname;




      But this creates an error during the execution of the constructor:



      Trying to get property of non-object


      Any ideas how to solve this? Thanks!







      constructor eloquent belongs-to






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:52

























      asked Nov 11 '18 at 23:20









      Tim K.

      257




      257






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You could change your getBrand function a little to the following to define the relationship between a Car and a Brand:



           public function brand() 
          return $this->belongsTo('AppBrand', 'FK_BrandId');



          The brand model can then be accessed from an instance of Car with, e.g. $car->brand. To access the brand name you can then use $car->brand->Brandname.



          If you'd like to access the brand name directly, you could define an "accessor" on the Car model like this:



          public function getBrandNameAttribute()

          return $this->brand->BrandName;



          You would then be able to access the brand name with $car->brand_name.






          share|improve this answer




















          • thanks for the idea with the Accessor
            – Tim K.
            Nov 15 '18 at 4:45










          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%2f53254233%2filluminate-eloquent-how-to-use-belongsto-in-constructor%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














          You could change your getBrand function a little to the following to define the relationship between a Car and a Brand:



           public function brand() 
          return $this->belongsTo('AppBrand', 'FK_BrandId');



          The brand model can then be accessed from an instance of Car with, e.g. $car->brand. To access the brand name you can then use $car->brand->Brandname.



          If you'd like to access the brand name directly, you could define an "accessor" on the Car model like this:



          public function getBrandNameAttribute()

          return $this->brand->BrandName;



          You would then be able to access the brand name with $car->brand_name.






          share|improve this answer




















          • thanks for the idea with the Accessor
            – Tim K.
            Nov 15 '18 at 4:45















          0














          You could change your getBrand function a little to the following to define the relationship between a Car and a Brand:



           public function brand() 
          return $this->belongsTo('AppBrand', 'FK_BrandId');



          The brand model can then be accessed from an instance of Car with, e.g. $car->brand. To access the brand name you can then use $car->brand->Brandname.



          If you'd like to access the brand name directly, you could define an "accessor" on the Car model like this:



          public function getBrandNameAttribute()

          return $this->brand->BrandName;



          You would then be able to access the brand name with $car->brand_name.






          share|improve this answer




















          • thanks for the idea with the Accessor
            – Tim K.
            Nov 15 '18 at 4:45













          0












          0








          0






          You could change your getBrand function a little to the following to define the relationship between a Car and a Brand:



           public function brand() 
          return $this->belongsTo('AppBrand', 'FK_BrandId');



          The brand model can then be accessed from an instance of Car with, e.g. $car->brand. To access the brand name you can then use $car->brand->Brandname.



          If you'd like to access the brand name directly, you could define an "accessor" on the Car model like this:



          public function getBrandNameAttribute()

          return $this->brand->BrandName;



          You would then be able to access the brand name with $car->brand_name.






          share|improve this answer












          You could change your getBrand function a little to the following to define the relationship between a Car and a Brand:



           public function brand() 
          return $this->belongsTo('AppBrand', 'FK_BrandId');



          The brand model can then be accessed from an instance of Car with, e.g. $car->brand. To access the brand name you can then use $car->brand->Brandname.



          If you'd like to access the brand name directly, you could define an "accessor" on the Car model like this:



          public function getBrandNameAttribute()

          return $this->brand->BrandName;



          You would then be able to access the brand name with $car->brand_name.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 22:06









          Delena Malan

          607415




          607415











          • thanks for the idea with the Accessor
            – Tim K.
            Nov 15 '18 at 4:45
















          • thanks for the idea with the Accessor
            – Tim K.
            Nov 15 '18 at 4:45















          thanks for the idea with the Accessor
          – Tim K.
          Nov 15 '18 at 4:45




          thanks for the idea with the Accessor
          – Tim K.
          Nov 15 '18 at 4:45

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53254233%2filluminate-eloquent-how-to-use-belongsto-in-constructor%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo