0
I’m using Laravel 5.8 and added an extra field in the user register. I want to insert a form select in this field but, I’m having difficulty.
What I thought I could do, was this in the file Registercontroller.php (\app\Http\Controllers\Auth
):
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
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');
$setores = \App\Setores::all();
return view('auth.register', compact('setores'));
}
/**
* 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:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'setor_id' => ['required'],
]);
}
/**
* 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']),
'setor_id' => $data['setor_id'],
]);
}
}
And that:
<select id="setor_id" name="setor_id">
@if(count($setores)>0)
@foreach($setores as $set)
<option value="{{ $set->id }}" selected="selected"> {{ $set->nome }}
</option>
@endforeach
@endif
In the archive Register.blade.php (\resources\views\auth
)
But, I’m getting the following error as return:
Facade Ignition Exceptions Viewexception
Undefined variable: sectors (View: C: xampp htdocs..._... Resources views auth Register.blade.php)
Does anyone know how I could pass this variable to my View register.blade.php
?
Are you sure
view('auth.register', compact('setores'))
should be in the__construct()
and not in the method relating toregister.blade
?– Erlon Charles
So. I did not find where this view is called. I will post all the contents of my Registercontroller.
– keniaferreira
Look for the method
register()
in classRegistersUsers
and overwrite this method in its classRegisterController
adding the variable$setores
at your sight– Erlon Charles
I found this Registerusers.php class and put this variable right in the View call. Thank you, Erlon!!!
– keniaferreira