Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

Populating dropdown in form from table

Hello,

I am a bit confused on how to use the controllers/models in the right way.

I have 2 tables:

-Companies and towns

I have created a form that shows here "company/create.blade.php":

public function create()
{
return view('company.create');
}

In this form I would like to populate the dropdown menu with towns from the town table.

I currently have a company model, controller and a town model and controller.

The form is showing fine but as soon as I try to pass the dropdown values from the town table, I receive errors(see below).

use App\Town;

public function create()
{
$towns = App\Town::all();
return view('company.create', ['towns' => $towns]);
}

I have also tried this but same error.:

public function create()
{

$towns = App\Town::all()->orderBy('name', 'desc')->get();
return view('company.create', compact('towns'));
 }

In the company/create.blade.php I added this:

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

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

I was expecting to have the form populated from the table "town" but instead, I am getting this:

ErrorException (E_ERROR) Undefined variable: towns (View: C:\laragon\www\kolloxmt\resources\views\company\create.blade.php)

Any idea what am I doing wrong please?

0 likes
9 replies
LaraBABA's avatar

Thanks, no same error: ErrorException (E_ERROR) Undefined variable: towns (View: C:\laragon\www\kolloxmt\resources\views\company\create.blade.php)

Select a Town

tisuchi's avatar

Because you are doing wrongly.

The problem is that, you are creating same issue in new discussion again and again without solving previous one.

Check my previous comment regarding that.

4 likes
LaraBABA's avatar

I am not sure I understand what you mean. I did add the code:

Ishatanjeeb's avatar

Please check this line

<div class="form-group{{ $errors->has('towns') ? ' has-error' : '' }}">

here you declare undefined variable.I don't know what kind of check you thinking.

tisuchi's avatar

@benoit1980

What I meant, you have created new discussion for same issue, more than 1 time.

The ideal way, you should create one discussion for one issue. Here is your last 2 discussions for same issue that you have created.

4 likes
LaraBABA's avatar

It is not the same issue, this is why I separated them. In Laravel the logic is separated from the design, so I thought perhaps this forum would be separated in the same way.

In one of the discussion I asked about Blade(design) as I consider blade to be part of the design.

While in the other issue I ask about the logic tself(php).

Hope this clarifies the issue.

Thank you.

LaraBABA's avatar

Regarding this line

No this is fine, it is from my courses online, all the fields have the same line, it simply add a css class of has-error if the variable is empty. It works great on all the fields. I have removed it just in case but the error persist. I will keep looking, perhaps the cache or something...

LaraBABA's avatar

this was missing.. use Illuminate\Support\Facades\DB;

Please or to participate in this conversation.