Support the ongoing development of Laravel.io →
Database Views Forms
Last updated 3 weeks ago.
0

try with foreach loop in view

foreach ($compaines as $companie) {
    <option value="<?php $companie[company_id] ?>">$companie[company name]</option>
}
Last updated 3 weeks ago.
0

Thanks for the response. Maybe I'm not doing it right, but I did this:

{{ Form::select('company_id', foreach ($companies as $companie) {
		<option value="<?php $companie[id] ?>">$companie[company_name]</option>
}
) }}

and got the error:

unexpected T_FOREACH

Did I use the foreach loop wrong?

Last updated 3 weeks ago.
0

i never use Form helper, only HTML, but try this:

$companies = RecordCompany::lists('company_name', 'id'); 

return View::make('admin.record_new')->with('companies', $companies);

{{ Form::select('company_id', $companies) }}
Last updated 3 weeks ago.
0

Thanks for the suggestion.

If I do that, I'm getting an error: Undefined variable: companies

And when I try the HTML that you suggested, I get: Use of undefined constant id

I keep getting the undefined variable error on other things that I'm working on as well.

What would cause those errors?

Last updated 3 weeks ago.
0

can you copy/paste your code?

Last updated 3 weeks ago.
0

Sure.

Here is from the controller:

$companies = RecordCompany::lists('company_name', 'id'); 

return View::make('admin.record_new')->with('companies', $companies);

Here is from the view:

{{ Form::select('company_id', $companies) }}

I also tried in the view:

<select name="company_id">
  foreach ($companies as $companie) {
    <option value="<?php $companie[company_id] ?>">$companie[company name]</option>
  }
</select>

The Form Helper option gives me the error "Undefined variable: companies". The non-Form Heler option gives me the error "Use of undefined constant id".

Did I do something wrong?

Last updated 3 weeks ago.
0

do you have company_id in db and try this $companie->compay_id $companie->company_name

Last updated 3 weeks ago.
0

When I do that it tells me "Undefined variable: companies".

Last updated 3 weeks ago.
0

Because it's saying that the undefined variable is companies, isn't it throwing the error at

foreach ($companies as $companie)
Last updated 3 weeks ago.
0

I think you're missing something when you're returning the View::make from the controller.

return View::make('admin.record_new')->with('companies', $companies);

Don't forget the part

with('companies', $companies);

This will provide the $companies (this name was used because of the first parameter 'companies') variable in your view, using the value of $companies in the controller.

Last updated 3 weeks ago.
0

Thanks, vernard. I've got that in the controller. That's what's baffling me. I've got exactly what you have there and it's still telling me that companies is undefined.

Any ideas why it would do that?

Last updated 3 weeks ago.
0

Out of interest, what happens if you dd($companies); before your return View::make?

Last updated 3 weeks ago.
0

I'm still getting the orange and gray "Whoops" screen. It's actually telling me though that companie is the undefined variable which, as above, is in the foreach loop:

foreach ($companies as $companie)
Last updated 3 weeks ago.
0

ChrisSoutham said:

Out of interest, what happens if you dd($companies); before your return View::make?

Good point. Maybe the $companies variable in the controller is already faulty before making it to the view.

If the $companies variable is fine, the form helper should work.

However, your code in the loop will still not work. Here's some of the problem I have noticed.

<select name="company_id">
  foreach ($companies as $companie) {
    <option value="<?php $companie[company_id] ?>">$companie[company name]</option>
  }
</select>

Is is possible to use a foreach without enclosing it in a "<?php ?>" tag?

<option value="<?php $companie[company_id] ?>">$companie[company name]</option>

You did not echo the $companie[company_id]. You just called in inside the php tag.

You should use enclose the company_id in quotes, making it a string. Unless you're using some kind of a constant, but I don't think that's the case.

And you're also using the "company name" as a key of the array. I think you're missing the underscore.

Try this.

<select name="company_id">
<?php
    foreach ($companies as $companie) {
?>
    <option value="<?php echo $companie['company_id'] ?>"> <?php echo $companie['company_name'] ?> </option>
<?php
    }
?>

</select>

I have not tested the code above. So please don't hurt me when you get an error. :)

Last updated 3 weeks ago.
0

I would never hurt you! Ha ha! I appreciate the help.

I tried what you suggested and am still getting "undefined variable: companie". I changed $companie to $company thinking that there might be an issue with the plural (thinking that Laravel changes plural to singular in controllers and models). When I do that, I'm getting "undefined variable: companies", which must mean that there's a problem in the controller. Right?

But I honestly don't know what it could be. I took the suggestion above. And it jives with the tutorials I've read.

I'm completely and utterly baffled (and frustrated).

Last updated 3 weeks ago.
0

It would be much of a help if you can put your code in the bin (Pastebin, not recycle bin. ha ha!) so that I can review them and point out possible errors.

Here's a link to bin. http://laravel.io/bin

Last updated 3 weeks ago.
0

I didn't even know about that. I posted to the bin.

Last updated 3 weeks ago.
0

in the same boat as you are in jerauf. relatively, new to laravel and trying to populate a dropdown via db entries.

i found a solution (might not be the best way).

  1. declare $companies to the function creating the view in the controller file.
  2. add declared value to the return
  3. check the http://laravel.com/api/ to code my select {{ Form::select('formName','values') }}

with 3 i did not have to use any html form code at all.

good luck

Last updated 3 weeks ago.
0

jerauf said:

I didn't even know about that. I posted to the bin.

When you paste to the bin you need to share the link it generates...

Last updated 3 weeks ago.
0

AndrewBNZ said:

jerauf said:

I didn't even know about that. I posted to the bin.

When you paste to the bin you need to share the link it generates...

Hahahah! I thought it was quite obvious and gave me a laugh when I saw this.

But we can learn from this. It is really useful if you can see the pastebin per user. So when you go to another user's profile, you may find the pastebins that he has posted.. Well, I think that should go to another thread.

I'll be waiting for your pastebin link.

Last updated 3 weeks ago.
0

I suppose you should have Companies db table with at least 2 fields with or w/o timestamps:

  • id
  • name
  • created_at
  • updated_at

Model (Company.php)

standard model

Controller (CompaniesController.php)

Making an array with [id] => 'name' and send to route index, which u can change

public function index() {
    return View::make('companies.index')
        ->with('companiesArray', Company::lists('name','id'));
}

View (companies/index.blade.php)

// creating a list of companies
{{ Form::select('company_id', $companiesArray) }}
Last updated 3 weeks ago.
0

Here is the pastebin link. Sorry about that.

http://laravel.io/bin/VLj0

Last updated 3 weeks ago.
0

Well I'm not sure that this returns something:

Company::lists('name','id'));

Try this:

Company::all()->lists('name','id'));
Last updated 3 weeks ago.
0

Use PHP's compact to pass an array containing variables and their values to the view:

  1. In the controller:

    $companies = RecordCompany::lists('company_name', 'id');

    return View::make('admin.record_new', compact('companies'));

  2. In your view file, you use the data passed to it like this:

    {{ Form::select('company', $companies) }}

Last updated 3 weeks ago.
0

That works! Thank you, byjml!

Last updated 3 weeks ago.
0

I'm facing the same porblem. I can share pastbin link here

http://laravel.io/bin/G2aJ

PLzzzzzzzzzzzzzzzzzzzzz help me.

Last updated 3 weeks ago.
0

jerauf said:

That works! Thank you, byjml!

Worked for me also... gotta love forums!

Last updated 3 weeks ago.
0

How do you retrieve distinct values from database?

0

Try my Laravel Package for Creating Dynamic, Database Driven, Bootstrap supported, Drop Down Menu.

GitHub.com/secrethash/dropmenu

Last updated 4 years ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

jerauf jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.