N
N
naneri2015-03-06 07:49:22
Laravel
naneri, 2015-03-06 07:49:22

How to change the data inside the model itself?

There are functions in the controller:

...
if($message->receiver_id == Auth::id()){

    // отмечает сообщение прочитанным
   $message->setWatched();
}
....

Now how can I change the object parameter inside the class method? I have tried code like this
static function setWatched(){  
              $this->watched = 1; 
     }

But it gives an error "Using $this when not in object context", you can of course re-pull the object's data from the database - but this is +1 query to the database. Or you can write logic directly in the controller - but this is Bad Practice. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-03-06
@naneri

Your method is declared static, which is indicated in the error. Remove static and everything will work.

R
Roman Sokharev, 2015-03-06
@greabock

public function setWatched()
{  
              $this->defineWatched(1);
}

public function defineWatched($value)
{
              $this->watched = $value;
}

That's better. You need two wrappers for a public property. One is not enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question