Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DewiH's avatar
Level 1

Laravel Livewire

Hello,

I'm learning laravel livewire, but I'm stuck trying to find a solution. I'd like to have

  • when a student is created, on submit it will Create a User with User's role as student.

For this, I've tried modifying the App\Http\Livewire\Student\Create.php :

class Create extends Component
{
		public Student $student;
        public $first_name,$last_name;

		public function mount(Student $student)
    		{
        		$this->student = $student;
        		$this->initListsForFields();
   		     }
         public function submit()
          {
              $this->validate();
              $this->student->save();

              $newUser = User::create([
                   'first_name'    => $this->first_name,
                   'last_name'     => $this->last_name,
              ]);
              $newUser->roles()->attach(Role::where('title', 'Student')->first());
            $this->student->update([
                       'user_id' => $newUser->id, 
                     'full_name' => $newUser->name
             ]);

            return redirect()->route('admin.students.index');
       }
}

But it's not working : it's redirecting me to the same page but with empty fields, and there's no error message. Can someone help me please?

Many thanks in advance,

0 likes
2 replies
Ben Taylor's avatar

Isn't your new user going to need an email address? And you should probably set a random password too.

Look in the network tab of your developer tools in the browser and see what response you are getting. Might be a clue there

DewiH's avatar
Level 1

I've set up the user's email as $table->string('email')->nullable()->unique(); The users can login using their username, which will be generate through UserObserver.

I've checked the Network tab in the browser developer tools , and I have this error : The GET method is not supported for route livewire/message/student.create. Supported methods: POST.

Please or to participate in this conversation.