Laravel 5.5 - How to add dropdown menu to auth.register

Published 1 month ago by Boubou

Hello,

I am really struggling on this.

I have created a model called Town with:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Town extends Model { protected $fillable = ['id']; // column id of table my_towns }

In the RegisterController I have added this:

use App\Town;

use Illuminate\Support\Facades\DB;

protected function showTowns(){

  $towns = DB::table('my_towns')->get();

  return view('auth.register', ['towns' => $towns]);

}

Register.blade

                 <div class="form-group{{ $errors->has('towns') ? ' has-error' : '' }}">
                 <label for="town_id" class="col-md-4 control-label">Select a Town</label>
                 <div class="col-md-6">

                      <select class="form-control" name="town_id" id="town_id">
                      <option value="">Select a Town</option>
                     @foreach ($towns as $town)
                      <option value="{{ $town->id }}">{{ ucfirst($town->name) }}</option>
                     @endforeach
                      </select>
                     @if ($errors->has('towns'))
                         <span class="help-block">
                             <strong>{{ $errors->first('towns') }}</strong>
                         </span>
                     @endif
                 </div>
             </div>

In the User.php model I have added this:

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable { use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'surname', 'username', 'email', 'password', 'town_id',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

My route in web.php

Route::get('auth.register', '[email protected]');

Now when I try to render the register.blade view I get this:

ErrorException (E_ERROR) Undefined variable: towns (View: C:\laragon\www\myproject\resources\views\auth\register.blade.php)

How can the "towns" variable shows as undefined please? I am lost on this one..

Thank you.

JackJones

Do you have Auth::routes() in your web.php file? Above this line by any chance:

Route::get('auth.register', '[email protected]');
Boubou
Boubou
1 month ago (12,670 XP)

Yes I do:

Auth::routes();

Route::get('home', '[email protected]')->name('home');

Route::get('auth.register', '[email protected]');

JackJones

Put it below that line

Boubou
Boubou
1 month ago (12,670 XP)

Thanks, Tried, same error unfortunately :-(

JackJones

die and dump in that Controller, your Auth::routes() route is being called most likely, and not your Controller method

Boubou
Boubou
1 month ago (12,670 XP)

I am not too sure about this one(still new to the framework).

Would this way be ok?

protected function showTowns(){

  $towns = DB::table('my_towns')->get();

  return view('auth.register', dd($towns));

}

I tried that but I am getting no output for the var_dump().

Boubou
Boubou
1 month ago (12,670 XP)

I have also tried this but does not help

Auth::routes('auth.register', function() {

$test = new Item;

dd($test);

Log::info($test); 

var_dump($test);

});

Boubou
Boubou
1 month ago (12,670 XP)

OK Finally found the problem by trialing many things, if someone could explain this to me it would be great.

In my RegisterController

I had things this way:

namespace App\Http\Controllers\Auth;

use App\User;

use App\My_town;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Validator;

use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Support\Facades\DB;


class RegisterController extends Controller
{

In this order my facade query was not working:

  $towns = DB::table('my_towns')->get();   

When I moved the below line to the top, it worked

use Illuminate\Support\Facades\DB;

as:

namespace App\Http\Controllers\Auth;

use App\User;

use App\My_town;

use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Validator;

use Illuminate\Foundation\Auth\RegistersUsers;


class RegisterController extends Controller
{

Why would this happen please?

Nothing wrong with my code apart from this order of name spaces.

Thanks

JackJones

You need to put some effort into learning OOP, PHP and if you choose to use Laravel then that too, there are no shortcuts unfortunately else we'd all take them, that would be my advice, other's may disagree

To answer your question, the order makes no difference

Boubou
Boubou
1 month ago (12,670 XP)

I have been programming in procedural for a long time. Moving to oop is a bit of a pain to be honest but I am trying.

Regarding the "use" line, I tried again and the query fails when placed lower down the list...I will have to accept this as bug from the framework itself.

Thanks again.

Please sign in or create an account to participate in this conversation.