0

I'm trying to create a user with my custom authentication and getting 500 error while posting data. I gues I messed something wrong with user model. Because If I trying to return the input from the form it's return succefull but If I'm trying to post data to DB I'm getting 500 error. ps .env is configured correctly. User model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
   
    protected $fillable = [
        'name', 'password',
    ];

}

Auth Controller:

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Models\User;
    
    class AuthController extends Controller
    {
       
 //Login function
     function register(){
        return view('auth.register');
    }

        //Create user in DB
        function create(Request $request){
    
            //Validate requests
            $request->validate([
                'name'=>'required',
                'password'=>'required'
            ]);
    
             //Insert data into database
             $user = new User;
             $user->name = $request->name;
             $user->password = Hash::make($request->password);
             $save = $user->save();
    
            
        }
    
       
        }

Routes:

**<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;



//Register Route
Route::get('/auth/register', [AuthController::class, 'register']);

Route::post('/auth/create', [AuthController::class, 'create'])->name('auth.create');**
6
  • Turn on the debugger and check what's the error! Commented Sep 1, 2021 at 11:30
  • We need to know what is the actual error. Set APP_DEBUG=true in your .env file or look in the logs (storage/logs/laravel.log) Commented Sep 1, 2021 at 11:30
  • Make APP_DEBUG=true, as your debugger is off Commented Sep 1, 2021 at 11:30
  • 3
    Your AuthController did not initialized use Hash;. This could be case for getting error. Commented Sep 1, 2021 at 11:32
  • 1
    @ApurvBhavsar that's right. He's not using use Hash, and as the debugger is off as well, so it's not throwing the error Commented Sep 1, 2021 at 11:33

1 Answer 1

1

Check if there are some columns not nullable or can't be empty.

Set APP_DEBUG=true at .env file you see errors.

If you are using the default user table

$table->id();
$table->string('name'); // can't be empty
$table->string('email')->unique(); // can't be empty
$table->timestamp('email_verified_at')->nullable();
$table->string('password'); // can't be empty
$table->rememberToken();
$table->timestamps();

UPDATE:

As apurv-bhavsar said you must add use Illuminate\Support\Facades\Hash; before the class

...
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash; // <<

class AuthController extends Controller
...
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.