1

I am new to laravel. I want to insert users data to database using registerController in laravel.

What I have tried is:

register.blade.php

@extends('adminlte::auth.auth-page', ['auth_type' => 'register'])

@php( $login_url = View::getSection('login_url') ?? config('adminlte.login_url', 'login') )
@php( $register_url = View::getSection('register_url') ?? config('adminlte.register_url', 'register') )

@if (config('adminlte.use_route_url', false))
    @php( $login_url = $login_url ? route($login_url) : '' )
    @php( $register_url = $register_url ? route($register_url) : '' )
@else
    @php( $login_url = $login_url ? url($login_url) : '' )
    @php( $register_url = $register_url ? url($register_url) : '' )
@endif

@section('auth_header', __('adminlte::adminlte.register_message'))

@section('auth_body')
 

 <?php  $res= DB::table('states')->orderBy('name','asc')->get(); 

 ?>
    <form method="POST" action="{{ route('register_user') }}" class="registerForm">
                       
 @csrf
        <div class="row">
            <div class="col-md-6">
                {{-- First Name field --}}
                

                <div class="col-md-6">
                <div class="input-group form-group">
                    <input type="text" class="form-control" placeholder="First Name *" name="first_name" value="" required>
                    
                </div>
            </div>
            </div>
            <div class="col-md-6">
                <div class="input-group form-group">
                    <input type="text" class="form-control" placeholder="Last Name *" name="last_name" value="" required>
                    
                </div>
            </div>
            <div class="col-md-6">
                <div class="input-group form-group">
                    <input type="email" class="form-control" name="email" value="" placeholder="Email *" required>
                    
                </div>
            </div>
            <div class="col-md-6">
                <div class="input-group form-group">
                    <input type="text" class="form-control phoneMask" placeholder="Phone *" name="phone" value="" required>
                    
                </div>
            </div>

        <div class="row">
            <div class="col-md-3 col-xs-offset-4 submit_btn">

            {{-- Register button --}}
        <button type="submit" class="btn btn-block {{ config('adminlte.classes_auth_btn', 'btn-flat btn-primary') }}">
            <span class="fas fa-user-plus"></span>
            {{ __('adminlte::adminlte.register') }}
        </button>

            </div>
        </div>
        </div>
    </form>


@stop

@section('auth_footer')
    <p class="my-0">
        <a href="{{ route('login') }}">
            {{ __('adminlte::adminlte.i_already_have_a_membership') }}
        </a>
    </p>
@stop

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use Auth;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users']
            //'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    /*protected function create(array $data)
    {
        /*return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
        dd($data);
         $user= User::create([
            'first_name'              => $data['first_name'],            
            'last_name'         => $data['last_name'],
            'email'             => $data['email'],
            'password'          => Hash::make($data['password']),
            'mobile'             => $data['phone']
            
        ]);

        // dd($data['password'], $user->password);
       return $user; 
        
}*/
public function registerUsers(Request $request)

{
   
    $first_name=$request->input('first_name');
     $last_name=$request->input('last_name');
       $email=$request->input('email');
         $phone=$request->input('phone');  

dd($request);
DB::insert('insert into users(first_name,last_name,email,phone)values(?,?,?,?)',[$first_name,$last_name,$email,$phone]);

}
    }

web.php

Route::get('/', function () {
    return view('auth.login');
});

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/register_user', 'Auth\RegisterController@registerUsers')->name('register_user');

Model

class User extends Authenticatable implements AuditableContract
{
    use HasApiTokens, Notifiable;
    use SoftDeletes;
    use Auditable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $fillable = ['first_name', 'last_name','email','phone','device_token','password','approved','added_by','removed','removed_by','deleted_at'];
}

When I try to submit this form, it is not inserting data to database and is showing HTTP error 500 and couldn't handle request. What my form looks like

Form

How to fix and insert data to database.

8
  • 1
    First thing you have to do is checking error logs (server logs, php logs and laravel logs). Commented Nov 29, 2021 at 9:24
  • @RouhollahMazarei How to do so Commented Nov 29, 2021 at 9:26
  • 2
    laravel logs - storage/logs/laravel.log most of the errors will be recorded in this file Commented Nov 29, 2021 at 9:29
  • @yadu siva das ,@RouhollahMazarei Is there any other method to fix this. I want to insert data to users table Commented Nov 29, 2021 at 9:32
  • give me a sec, i'll write the function for you Commented Nov 29, 2021 at 9:35

3 Answers 3

0

Try to add the cross-fire request forgery to your form. Generally I get this error when I forget it. Add it like that in your view:

<?php  $res= DB::table('states')->orderBy('name','asc')->get(); ?>
<form method="POST" action="{{ route('register_user') }}" class="registerForm">
 
@csrf             

    <div class="row">
        <div class="col-md-6">
            {{-- First Name field --}}
Sign up to request clarification or add additional context in comments.

2 Comments

Its already included
@Binsha where ? I don't see it in your view. I even run a key word searched.
0

Route

 Route::post('/test', 'Auth\RegisterController@test')->name('test');

Auth\RegisterController

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Validator;
. 
.
.


public function test ( Request $request){

        $rules = array(
        'first_name' => 'required', 'string', 'max:255',
        // add validation rules here
    );

    $validator = Validator::make($request->all(), $rules);
    if ($validator->passes()) {
        $user = new User();
        $user->name = $request->input('first_name');

        //copy above line and add remaining fields.

        $user->save();
        return redirect()->back()->with(array('message' => 'user added.'));
    } else {
        return redirect()->back()->withErrors($validator)->withInput();
    }
}

7 Comments

Change route name,url and function name
Not working , showing same error as could not handle request.
I am using adminlte
Its not working. I am using adminlte
by default password is a mandatory field. change that in DB, and try again
|
0

follow these steps

  1. make sure your request received by the desired route
  2. make sure you have passed validation
  3. then input data like this

DB::table('post')->insert([

DB::table('posts')->insert([
'name_name' => \request()->name,
'last_name' => \request()->last_name,
'email' => \request()->email,
'phone' => \request()->phone,
]);

8 Comments

Not working . Not inserting to database
please follow the steps I defined and determine where is the problem?
Not inserting to database
till which step your code works? send the output of that step and share the code after that step
Not working till now dd($request) is working , but not inserting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.