Sameer Kamran
Sameer Kamran

Reputation: 247

Laravel Class/Controller level variable doesnt get update from Method

I have this variable on class/Controller level $songRating and i am calling this method through ajax, when i first time call this method it runs the if block which is good. and now $songRating should be 1. But this is not a case. When i call this method it again runs the if block. Dont know why :/

public $songRating;
public function GetHighRatedSong()
{
        if($this->songRating == null){
            $this->songRating=1;
        }else{
            $this->songRating=2;
        }

        return response()->json($this->songRating);
}

Upvotes: 0

Views: 211

Answers (2)

roden artuyo
roden artuyo

Reputation: 1

It's because everytime you call that function or make a new instantiation to the class, it will automatically reset to it's original value. Try to use service container

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

Try with Replacing

$this->songrating=1;

to

$this->songRating=1; # song+Rating != song+rating

Read PHP & Case Sensitivity

Upvotes: 2

Related Questions