I'm trying to customize the register of Laravel and I have this working but I need to add in some fields the last ID registered. In my actual code it registers all OK, but no login automatic after register.
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 = RouteServiceProvider::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, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:ssn'],
'account' => ['required', 'string', 'max:255', 'unique:user_auth'],
'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)
{
$ssn1 = mt_rand(1000000,9999999);
$ssn2 = mt_rand(100000,999999);
$ssn = $ssn1 . $ssn2;
$user_auth = User::create([
'account' => $data['account'],
'password' => DB::raw("CONVERT(BINARY(16), ".l2off($data['password'])." )"),
'quiz1' => $data['quiz1'],
'quiz2' => $data['quiz2'],
'answer1' => DB::raw("CONVERT(BINARY(16), ".l2off($data['answer1'])." )"),
'answer2' => DB::raw("CONVERT(BINARY(16), ".l2off($data['answer2'])." )"),
]);
$userId = $user_auth->id;
$ssn_info = ssn::create([
'ssn' => $ssn,
'name' => $data['name'],
'email' => $data['email'],
'job' => $data['job'],
'phone' => $data['phone'],
'zip' => $data['zip'],
'addr_main' => $data['addr_main'],
'addr_etc' => $data['addr_etc'],
'account_num' => $user_auth->id,
]);
$user_info = user_info::create([
'account' => $data['account'],
'ssn' => $ssn,
'kind' => "99",
]);
$user_account = user_account::create([
'account' => $data['account'],
'pay_stat' => "1",
]);
}
}
but if change it one to
return User::create([
'account' => $data['account'],
'password' => DB::raw("CONVERT(BINARY(16), ".l2off($data['password'])." )"),
'quiz1' => $data['quiz1'],
'quiz2' => $data['quiz2'],
'answer1' => DB::raw("CONVERT(BINARY(16), ".l2off($data['answer1'])." )"),
'answer2' => DB::raw("CONVERT(BINARY(16), ".l2off($data['answer2'])." )"),
]);
It register and automatically logs in, but I don't understand the last ID in the follow sequences.
I solved this by adding return $user_auth; at the end.
Related
I've tried to create an admin module for a schoolproject with laravel.
I copied all the authentication stuff to register a person, but changed everything so it's a a different module. However, I can't seem to fix this problem. It always says that my 'created_at' doesn't have a default value. But it has the timestamp() module so I would think that it doesn't need anything else. Anyone who sees the fault.
My Migration:
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('admin_id');
$table->string('naam');
$table->string('email')->unique;
$table->binary('password');
$table->enum('rol', ['user','admin'])->default('admin');
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
};
My model
class Admin extends Model
{
use HasFactory;
protected $table = 'admins';
protected $primaryKey = 'admin_id';
protected $fillable =
[
'admin_id',
'naam',
'email',
'password',
'rol',
];
public $timestamps = false;
}
My Controller
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\RedirectResponse
*
* #throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', 'min:5'],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user));
Auth::login($user);
return redirect(RouteServiceProvider::HOME);
}
public function createAdmin()
{
return view('auth.registeradmin');
}
public function storeAdmin(Request $request)
{
$request->validate([
'naam' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', 'min:5'],
]);
$admin = Admin::create([
'naam' => $request->naam,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($admin));
Auth::login($admin);
return redirect(RouteServiceProvider::HOME);
}
}
This is my RegisterController (Laravel ui edited). please help me.
Is i have to add any other page to this question.
protected function validator(array $data)
{
return Validator::make($data, [
'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\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
There is 2 case if name_of_firm is nullable or required
Case : 1
if name_of_firm is required
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'name_of_firm' => ['required']
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'],
'number' => $data['number'],
'address' => $data['address'],
]);
}
Case : 2
if name_of_firm is nullable
protected function validator(array $data)
{
return Validator::make($data, [
'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\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => $data['name_of_firm'] ?? null, // make sure database is accept null value
'number' => $data['number'],
'address' => $data['address'],
]);
}
you should use data_get or Arr::get() helpers to solved not exists element:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'name_of_firm' => data_get($data, 'name_of_firm'),
'number' => data_get($data, 'number'),
'address' => data_get($data, 'address')],
]);
}
I created a registration form with the use of laravel ui scaffolding.
What I did:
• Added some field besides the default ones
• Using the auth controller specifically RegisterController and change a part of the create function from this
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Into this:
protected function create(array $data)
{
return User::create([
'user_email' => $data['email'],
'user_Type' => $data['user_Type'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'contact_number' => $data['contact_number'],
]);
}
But the result after submitting the form with a post method is this:
ArgumentCountError
Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed in /Users/idan/Documents/SweetSurrender/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 1 expected
Web.php
Route::get('/register', function() {
return view('auth.register');
})->name('register');
Route::post('/register', [RegisterController::class, 'create']);
RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
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 = RouteServiceProvider::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
*/
public function index() {
return view('auth.register');
}
protected function validator(array $data)
{
return Validator::make($data, [
'username' => ['required', 'string', 'max:16'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'contact_number' => ['required', 'string', 'min:12', 'unique:users'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'user_email' => $data['email'],
'user_Type' => $data['user_Type'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'contact_number' => $data['contact_number'],
]);
}
}
I also tried using this in RegisterController.php
protected function create(Request $data)
{
return User::create([
'user_email' => $data->input('email'),
'user_Type' => $data->input('user_Type'),
'username' => $data->input('username'),
'password' => Hash::make($data->input('password')),
'contact_number' => $data->input('contact_number'),
]);
}
but it results to an error:
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'user_email' doesn't have a default value (SQL: insert into users (password, updated_at, created_at) values ($2y$10$vr7UasRBCzx2URQxLSWneuvr4Hl1at.q7qDk8UK0Wc/INjeHeYH7y, 2020-12-22 07:15:41, 2020-12-22 07:15:41))
I solved this problem by using the same code:
protected function create(array $data)
{
return User::create([
'user_email' => $data['email'],
'user_Type' => $data['user_Type'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'contact_number' => $data['contact_number'],
]);
}
and In the User model I included this:
protected $fillable = [
'username',
'user_email',
'password',
'contact_number',
];
I'm passing data to these functions via Axios/Vue. The Eloquent interactions work perfectly. When I store (i.e. create a new call) the resource returns as expected. When I update a record, it updates in the database, however, I get a blank response. In other words the return new CallResource($call) returns nothing. I can't work out where I've gone wrong.
public function store(Request $request)
{
$call = $this->validate($request, [
'title' => 'required',
'job_id' => 'required',
'location' => 'required',
'starts' => 'required|date|before:ends',
'ends' => 'required|date|after:starts',
'rate' => 'required'
]);
$call = Call::create($call);
return new CallResource($call);
}
public function update(Request $request, $id)
{
$data = $this->validate($request, [
'title' => 'required',
'job_id' => 'required',
'location' => 'required',
'starts' => 'required|date|before:ends',
'ends' => 'required|date|after:starts',
'rate' => 'required'
]);
$call = Call::find($id);
$call->update($data);
return new CallResource($call);
}
The call resource is really simple
class CallResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'location' => $this->location,
'starts' => $this->starts,
'ends' => $this->ends,
'rate' => $this->rate
];
}
}
Laravel 5.6 fyi.
my controller is RegisterController which is in controller in auth folder
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
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, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(Request $request)
{
$this->validate($request, [
'first_name' => 'required|max:255',
'middle_name' => 'string|max:255',
'last_name' => 'required|max:255',
'email' => 'required|max:255|unique:users',
'password' => 'required|min:6|',
'user_status' => 'required|max:255',
'user_role' => 'required|max:255',
'user_type' => 'required|max:255',
'phone' => 'required|max:255',
'address' => 'required|max:255',
]);
return User::create([
'first_name' => $request['first_name'],
'middle_name' => $request['middle_name'],
'last_name' => $request['last_name'],
'email' => $request['email'],
'password' => bcrypt($request['password']),
'user_status' => $request['user_status'],
'user_role' =>$request['user_role'],
'user_type' =>$request['user_type'],
'phone' =>$request['phone'],
'address' =>$request['address'],
]);
}
public function index(){
return User::all();
}
}
And my api.php file in routes folder is
Route::get('/users','Auth\RegisterController#index')->middleware('auth');
I want to all data from user table which code is in Auth\Regitercontroller in index function this route returns
{
"error":"unauthenticated",
}
so i want aall user from table user and if i remove middleware->('auth:api') from routes then i got all users but i want middleware in routes for security reason so help me.Thank You