Illuminate/Eloquent: how to use belongsTo in constructor
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
add a comment |
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
add a comment |
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
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
constructor eloquent belongs-to
edited Nov 12 '18 at 14:52
asked Nov 11 '18 at 23:20
Tim K.
257
257
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
.
thanks for the idea with the Accessor
– Tim K.
Nov 15 '18 at 4:45
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%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
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
.
thanks for the idea with the Accessor
– Tim K.
Nov 15 '18 at 4:45
add a comment |
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
.
thanks for the idea with the Accessor
– Tim K.
Nov 15 '18 at 4:45
add a comment |
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
.
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
.
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
add a comment |
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
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.
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.
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%2f53254233%2filluminate-eloquent-how-to-use-belongsto-in-constructor%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