phplaraveleloquentmutators

doesn't have a default value error with set mutator And update observer does not working


In saving a model, I have an error thats the slug attribute doesn't have a default value. I had created a setSlugAttribute mutators but it gived me the error again.

//Controller save method inside 
* * *
Task::create($request->all());
* * *

//Task model
public function setSlugAttribute(){
    $this->attributes['slug'] = Str::slug($this->title, '-');
}

How can I fix it? It does fix by using observe(saving), doesn't it? Another idea?

I have created an TaskObserver and I set it in ServiceProvider.

In the observer, updated(Task $task) and updating(Task $task) methods didn't work ! But the created method works.

//Update method inside:
$array = $request->all(['title', 'description', 'deadline', 'budget','guest_token']);
$task = Task::where('id',$request->id)->update($array);
//I am waiting working of udate observer but it don't

//TaskObserver
public function updating(Task $task)
{
    dd("updating");
}
public function updated(Task $task)
{
    dd("updated");
}

Solution

  • I have solved this problem by doing the following:

    //when I assigned it to a variable and then calling update method, it worked
    $task = Task::where('id',$request->id)->first();
    $update = $task->update($request->all(['title', 'description', 'deadline', 'budget','guest_token']));