0
votes

Iam new with Laravel trying to add user type in register blade page as drop down from my database but it give me every time the same error Undefined variable: uTypes (View: /Applications/MAMP/htdocs/creativeapp/resources/views/auth/register.blade.php)

The Route code web.php:

Route::get('/auth/register', 'UsersController@index');

The controller code UsersController.php:

 <?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class UsersController extends Controller {
    public function index() {
        $uTypes = DB::table('userstype')->pluck("type_name", "id");
        return view('auth.register', ['uTypes' => $uTypes]);
    }
}

The view code register.blade.php:

<div class="form-group row">
                        <label for="usertype" class="col-md-4 col-form-label text-md-right">{{ __('User Type') }}</label>

                        <div class="col-md-6">


                                <select name="userType" class="form-control">
                                    <option value="">--Select Type--</option>
                                    @foreach ($uTypes as $ut => $value)
                                    <option value="{{ $ut }}"> {{ $value }}</option>
                                    @endforeach
                                </select>

                        </div>
                    </div>

Route web.php

error

register.blade.php

usercontroller

1
Did you use artisan auth ?Saromase
@ Saromase Yes i didMohamad Salama
Your codes seem correct. Try to put fake values in $uTypes as ['uTypes' => ['Type 1' => 1, 'type2' => 2]]. But this behavior is weird. Also try clearing cache and views with php artisan view:clear and php artisan cache:clear.KeitelDOG
Does this DB::table('userstype')->pluck("type_name", "id") actually returns something ?Clément Rigo
You must try to echo something in your controller index method to see if the codes have even run in there. This way you will know what to change.KeitelDOG

1 Answers

0
votes

You dont need to rewrite Route, use auth native route.

You need to overwrite the getRegister method into RegisterController.

app/Http/Controllers/Auth/RegisterController.php

/**
 * Show the application registration form.
 *
 * @return \Illuminate\Http\Response
 */
public function getRegister()
{
    $uTypes = DB::table('userstype')->pluck("type_name", "id");
    return view('auth.register', ['uTypes' => $uTypes]);

}

And after that, dont forget to update validator, migration and create method