php - laravel form, data not storing into the database
one text
this is my blade file @csrf name @if ($errors->has('name')) {{ $errors->first('name') }} @endif
<div class="form-group col-md-6">
<label>email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter your email" required autofocus>
@if ($errors->has('email'))
<span class="Enter properly">{{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group col-md-6">
<label>Mobile-no</label>
<input type="text" name="mobile-no" class="form-control" id="mobile_no" placeholder="Enter your mobile no" required>
@if ($errors->has('mobile-no'))
<span class="Enter 10digits">{{ $errors->first('mobile_no') }}</span>
@endif
</div>
<div class="form-group col-md-6">
<label>Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="Enter your password" required>
@if ($errors->has('password'))
<span class="Enter properly">{{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group col-md-6">
<label>Confirm-password</label>
<input type="password" name="confirm_password" class="form-control" id="confirm_password" placeholder="Re-enter your password" >
</div>
<div class="form-group col-md-6" align="center">
<button class="btn btn-success" style="width:80px;">Submit</button>
</div>
</div>
</div>
</form>
@endsection
This is my controller file
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\FormController;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades;
class FormController extends Controller
{
public function index()
{
return view ('login');
}
public function postLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (User::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('registration');
}
public function postRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'mobile_no' => 'required',
'password' => 'required|min:6',
'confirm_password' => 'required',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("dashboard")->withSuccess('have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function dashboard()
{
if(User::check()){
return view('dashboard');
}
return redirect("login")->withSuccess('are not allowed to access');
}
public function signOut()
{
Session::flush();
User::logout();
return Redirect('login');
}
}
//this is my route file
Route::get('dashboard', [FormController::class, 'dashboard']);
Route::get('login', [FormController::class, 'index'])->name('login');
Route::post('post-login', [FormController::class, 'postLogin'])->name('login.post');
Route::get('registration', [FormController::class, 'registration'])->name('register');
Route::post('post-registration', [FormController::class, 'postRegistration'])->name('register.post');
//this is my migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('customRegistration', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('email');
$table->integer('mobile_no');
$table->string('password');
$table->string('confirm_password');
$table->timestamps();
});
}
public function down()
{
Schema::drop('form');
}
}
i created simple laravel login & registration form.laravel is not genrating errors. but it is not stored a data into the database i.e any of data will be not stored in database.
Source