Monday, April 10, 2017

Laravel routes, form won't submit data into the database

beginner trying to submit a basic form into the database using laravel. I know I should be using 'get' to display the registerform but when I do that I get the 'methodnotallowedhttpexception error.

Currently when I enter data into the form and press submit it just refreshes the page. Any help would greatly be appreciated, thanks

web.php

Route::post('registerForm', 'AuthController@viewregisterForm');

Route::post('registerUser', 'AuthController@registerUser');

AuthController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

//use Illuminate\Support\Facades\Auth;

//use App\Http\Requests;

use App\User;

class AuthController extends Controller
{
function viewregisterForm()
{
     return view('register/registerForm');
}
function registerUser(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required',
        'password' => 'required',
        'dateofbirth' =>'required',
    ]);
    //create a Film object
    $user = new $User();
    $user->name = $request->name;
    $user->email = $request->email;
    $user->password = $request->password;
    $user->dateofbirth = $request->dateofbirth;
    $user->role = 1;

    $user->save();
    return redirect('all');
}
}

?>

registerForm.blade

@extends('layouts.master')
@section('title', 'Register user')
@section('content')

<form action="" method="POST">

<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>

<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>

<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">

<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>



<input type="submit" name="submitBtn" value="Add User">
</form>
@endsection



via TF120

Advertisement