samedi 9 septembre 2017

Laravel5 ORM update - undefined property from variable

Description: I am creating a game, where you can upgrade your buildings. After clicking on any building, it will be upgraded to higher level by upgradeBuilding() method. The most problematic part is that I have to upgrade a building that will be in $building variable, it is always a string with name of the building. I think that the $currentBuildingLevel->{$building} is causing the error. The second problem is that whenever I try var_dump($currentBuildingLevel) its loading crazy long, its not just a simple object with levels of the buildings...

Database: In database buildings table, the levels look like this: enter image description here

Building model: Here is the code that should update a building level in above record:

$building = "barracks"  // example building

public function upgradeBuilding($building)
{
     $currentBuildingLevel = $this->first()->where("user_id", "=", Auth::id());     
     $currentBuildingLevel->{$building} = $currentBuildingLevel->{$building} + 1;
     $currentBuildingLevel->save();
}

Error: I am getting an error:

Undefined property: Illuminate\Database\Eloquent\Builder::$barracks

Question: How do I do this properly? Is $currentBuildingLevel an object already?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire