try with foreach loop in view
foreach ($compaines as $companie) {
<option value="<?php $companie[company_id] ?>">$companie[company name]</option>
}
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?
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) }}
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?
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?
do you have company_id in db and try this $companie->compay_id $companie->company_name
When I do that it tells me "Undefined variable: companies".
Because it's saying that the undefined variable is companies, isn't it throwing the error at
foreach ($companies as $companie)
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.
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?
Out of interest, what happens if you dd($companies); before your return View::make?
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)
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. :)
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).
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
I didn't even know about that. I posted to the bin.
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).
with 3 i did not have to use any html form code at all.
good luck
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...
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.
I suppose you should have Companies db table with at least 2 fields with or w/o timestamps:
standard model
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'));
}
// creating a list of companies
{{ Form::select('company_id', $companiesArray) }}
Here is the pastebin link. Sorry about that.
Well I'm not sure that this returns something:
Company::lists('name','id'));
Try this:
Company::all()->lists('name','id'));
Use PHP's compact
to pass an array containing variables and their values to the view:
In the controller:
$companies = RecordCompany::lists('company_name', 'id');
return View::make('admin.record_new', compact('companies'));
In your view file, you use the data passed to it like this:
{{ Form::select('company', $companies) }}
I'm facing the same porblem. I can share pastbin link here
PLzzzzzzzzzzzzzzzzzzzzz help me.
jerauf said:
That works! Thank you, byjml!
Worked for me also... gotta love forums!
Try my Laravel Package for Creating Dynamic, Database Driven, Bootstrap supported, Drop Down Menu.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community