Laravel - Passing Variable To View - Record Page

Asked

Viewed 1,881 times

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 to register.blade?

  • So. I did not find where this view is called. I will post all the contents of my Registercontroller.

  • 1

    Look for the method register() in class RegistersUsers and overwrite this method in its class RegisterController adding the variable $setores at your sight

  • I found this Registerusers.php class and put this variable right in the View call. Thank you, Erlon!!!

2 answers

0


The RegisterController implements the trait RedirectsUsers, in this trait she implements the method showRegistrationForm() which is the method that is called when you access the route /register, then just overwrite the method within the RegisterController, would look something like this:

public function showRegistrationForm()
{
    $setores = Setor::all();

    return view('auth.register', compact('setores'));
}

0

So, dude, the way I know how to do this is to define in the view controller the variables that you want to pass in the view. I usually do it this way:

//Crio uma função INDEX para carregar a tela:

public function index() {

//aqui faço toda a lógica e tratativa, e após isso dou um return:

return view('sua_view.index', [
         'variavel1' => $variavel_definida_na_lógica_acima
        , 'variavel2' => $variavel_definida_na_lógica_acima
        , 'variavel3' => $variavel_definida_na_lógica_acima
        , 'variavel4' => $variavel_definida_na_lógica_acima
        , 'variavel5' => $variavel_definida_na_lógica_acima
    ]);
}

After you adjust the view and the return of the variables, the mentioned error (Undefined variable:) disappears, and you can call the variables within the view, which in case would be

{{variavel1}} / {{variavel2}} / {{variavel3}} / {{variavel4}} / {{variavel5}}

Then to make the foreach work, don’t forget to obviously assign in the variables the type array, and then just follow what you were already doing. It works perfectly with me, and I honestly think it’s even more organized. I hope to help.

  • So it doesn’t work for me. I put the content in the View Register above.

  • Controllers generated by Auth are different. Not like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.