V
V
Vladislav2018-05-25 15:41:28
Laravel
Vladislav, 2018-05-25 15:41:28

How to change the values ​​of one column while adding another?

There is a questions table that contains questions from users. Default status = No response. So, when the admin adds the answer to the question (the body column), the status itself should change to published.
Laravel just recently started learning, thanks to everyone for the advice.
Sample table prntscr.com/jmhhe1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Dergachov, 2018-05-25
@vos_50

Create an observer

class UserObserver
{
  /**
   * Прослушивание события обновление вопроса
   *
   * @param  Question $question
   * @return void
   */
  public function updating(Question $question)
  {
       if ($question->body != $question->getOriginal('body ')) {
           $question->status = 'опубликован';
        }
  }
}

P
PashaNedved, 2018-05-25
@PashaNedved

For the model, add an answer() method that will call update() or save()

public function answer(string $body)
{
return $this->update(['body' => $body, 'status' => 'Опубликован']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question