php - LARAVEL: I want to import informations from another table and add it to the registration Form
Solution:
You need to do two things.
I am assuming you are using the default auth scaffolding. If you look into the RegistersUsers trait on your RegistrationController you will see that laravel is using the showRegistrationForm method to display the registration form. In your RegistrationController class you need to override this method to display your custom registration form. You can do this like so:
/**
* Show the application registration form.
*
* @return \Illuminate\View\View
*/
public function showRegistrationForm()
{
$departements = Departement::all();
return view('auth.register', [ 'departements' => $departments ]);
}
Secondly, you will need to customize the blade file located at ./resources/views/auth/register.blade.php. Since you only provided the snippet for the dropdown, I will inline it into a default auth.register blade file here.
<form method="POST" action="/auth/register">
{!! csrf_field() !!}
<div>
Name
<input type="text" name="name" value="{{ old('name') }}">
</div>
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password">
</div>
<div>
Confirm Password
<input type="password" name="password_confirmation">
</div>
<div class="form-group-row">
<label for="departements" class="col-md-4 control-label text-md-right">Départements :</label>
<div class="col-md-6">
<select name="departements" id="departements" class="form-control">
@foreach($departements as $departement)
<option value="{{ $departement ->id }}">{{ $departement ->name }}</option>
@endforeach
</select>
</div>
</div>
<div>
<button type="submit">Register</button>
</div>
</form>
Answer
Solution:
Did you define $departements from the loop in the view or pass it from controller? If not, assuming you have user passed in the view try this.
@foreach($user->departements() as $departement)
Source