deepu07
639
15
Laravel

Laravel observer BadMethodCallException Method Illuminate\Database\Eloquent\Collection::save does not exist.

Posted 10 months ago by deepu07

Hi folks, can anyone help me out on this? I'm getting an error in laravel observer. I have both controller and observer. where controller logic helps me to create a professor record. I wanna use observer to generate a roll_number based on professor record and want to update in same table.

Here is my code CONTROLLER

        $professor->first_name =  Input::get('first_name');
        $professor->middle_name =  Input::get('middle_name');
        $professor->last_name =  Input::get('last_name');
        $professor->dob =  Input::get('dob');
        
            $professor->save();`

OBSERVER
      `public function created($id)
    {
        $professor = Professor::findOrFail($id);
        $professor->first_name = Input::get('first_name');
        $professor->last_name = Input::get('last_name');
        $professor->dob =  Input::get('dob');

        $firstNameLetter = strtoupper(substr($professor->first_name, 0, 1));
        $lastNameLetter = substr( $professor->last_name, 0, 1);
        $dateOfBirth = substr($professor->dob, 0,4);

        $rollNumber = $firstNameLetter . $lastNameLetter . '@' . $dateOfBirth;

        $professor = Professor::find($id);
        $professor->roll_number = $rollNumber;
        $professor->update();
    }```

HERE Everything im getting a data to my observer but unable to save in DB. getting An error like this for both save() and update()

BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::save does not exist.

Thanks for advance.

Please sign in or create an account to participate in this conversation.