0

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

9
  • Did you use artisan auth ? Commented Mar 11, 2019 at 15:09
  • @ Saromase Yes i did Commented Mar 11, 2019 at 15:11
  • 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. Commented Mar 11, 2019 at 15:11
  • Does this DB::table('userstype')->pluck("type_name", "id") actually returns something ? Commented Mar 11, 2019 at 15:13
  • 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. Commented Mar 11, 2019 at 15:29

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Can you show us, the result of dd($uTypes) before return view ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.