I've got an issue Laravel, when I use get...Attribute
-function.
In my case I want to differentiate, if I can load the desired value from the database or if I need to return a custom object created within the code.
This is the code of the getComponentACltAttribute
function of my Verification
-class:
public function getComponentACltAttribute($value) {
if($this->superstructure_type == \Config::get('constants.superstructureTypes.standard')) {
return \App\Models\Werte\Product\Brettsperrholz::find($value);
}
else if($this->superstructure_type == \Config::get('constants.superstructureTypes.custom')) {
$customCltA = \App\Models\Werte\Product\BrettsperrholzBenutzerdefiniert::where('schichten_s',$this->component_a_number_of_layers)->where('decklagen_fk', $this->component_a_cover_layer->id)->first();
if($customCltA != null) {
$componentACustomClt = \App\Models\Werte\Product\Brettsperrholz::newModelInstance();
$componentACustomClt->id = 0;
$componentACustomClt->bezeichnung = '';
$componentACustomClt->hersteller_fk = 0;
$componentACustomClt->dicke_d = $this->component_a_thickness;
$componentACustomClt->schichten_s = $this->component_a_number_of_layers;
$componentACustomClt->decklagen_fk = $this->component_a_cover_layer;
$componentACustomClt->t_1 = $this->component_a_t_1;
...
$componentACustomClt->t_11 = $this->component_a_t_11;
$componentACustomClt->o_1 = $customCltA->o_1;
...
$componentACustomClt->o_11 = $customCltA->o_11;
$componentACustomClt->material = 'C24';
$componentACustomClt->zulassung_fk = 0;
return $componentACustomClt;
}
}
}
The component_a_clt
object is an object of the Brettsperrholz
class and has a property called decklagen_fk
which should be automatically loaded, when an object is loaded from the database (being automatically loaded defined within $with
):
<?php
namespace app\Models\Werte\Product;
class Brettsperrholz extends \App\Models\Base\BaseModel
{
public $table = "tbl_werte_product_brettsperrholz";
protected $hidden = [
];
protected $with = [
'decklage',
];
public function decklage() {
return $this->hasOne(\App\Models\Werte\Product\Decklage::class, 'id', 'decklagen_fk');
}
}
Now I'm having issues with the property component_a_clt
of the Verification
class on other code areas. I.e. I have this piece of code somewhere in my code:
if($reportData["component_" .$component ."_clt"]->decklagen_fk == \Config::get('constants.coverLayer.double')) {...
...where $reportData
is the object of Verification
class.
When I return the custom created object of component_a_clt
, this piece of code does not work. Then I need to use
if($reportData["component_" .$component ."_clt"]->decklagen_fk->id == \Config::get('constants.coverLayer.double')) {...
But when I return the object from the database, I need to write the if
-statement as mentioned in my first example. Why is the handling so differently and what can I do to make it equivalent in behaviour?
Although you don't actually say what it is you are trying to achieve, from the if
statements shown:
if (
$reportData["component_" .$component ."_clt"]
->decklagen_fk->id == \Config::get('constants.coverLayer.double')
) {
you want to know if a particular condition is true about some data related to your object.
So instead of getting the relationship from a custom attribute getter (which has other issues, such as returning different types of data) and then compare some attribute to a constant in an if
condition, why not create a function on the original object to test the condition and return a boolean result?
That way you can hide all the messy details of whether a Brettsperrholz
or BrettsperrholzBenutzerdefiniert
model is returned, and avoid having to to write different conditional tests everywhere.
You don't say what the name of the class is where the getComponentACltAttribute
function is declared, so I'll call it MainClass
, and this code is not complete, it's just to get the idea across...
class Mainclass
{
public function isDoubleLayered(): bool
{
// Do all the stuff from your attribute getter function to
// know if you are testing 'decklagen_fk' or 'decklagen_fk->id'
...
return decklagen_fk->id == \Config::get('constants.coverLayer.double');
}
}
Which makes your if
conditions easier
if ($reportData["component_" .$component ."_clt"]->isDoubleLayered()) {
...
}