I've constructed a select list in a dashboard view, which lists id and name of various components. The data is passed to a controller that makes a view using the passed id to pull up the correct component data for that id. Problem is that the controller is constructed to expect an object from which to get the id, so that when I submit the id from the list, I get a "Trying to get property of non-object" error. (It doesn't matter whether I submit to the route or directly to the controller; I get the same error.) Here's the code:
PagesController (that creates list array for the dashboard):
public function showDashboard()
{
$components = Component::lists('name','id');
return View::make('dashboard', array(
'components'=>$components, ...
));
}
Snippet of source code for the select list:
<form method="GET" action="https://..." accept-charset="UTF-8">
<select id="id" name="id"><option value="2">Component Name</option>...
Components Model:
class Component extends Eloquent {
protected $table = 'components'; ... }
ComponentsController:
public function show($id)
{
$component = $this->component->find($id);
return View::make('components.show', array(
'component'=>$component, ...
));
}
dashboard.blade.php:
{{ Form::open(array(
'action' => 'ComponentsController@show',
'method'=>'get'
)) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::close() }}
The same controller code is used for other purposes and works fine, for example, when I pass a specific id from a URL, it accepts that id without an error. So, I know this should be something simple involving the form opening, but I can't figure it out. How can I fix this? Thanks!
Maybe this works
replace
$components = Component::lists('name','id');
with
$components = Component::all()->lists('name','id');
Thanks, but nope -- that still gets a "Trying to get property of non-object" error when I hit submit. It creates the drop-down list just fine, exactly the same as without interposing the all(). But it doesn't change the essential problem, which is that selecting a value from a drop down list sends a single value, not an object. And I can't figure out the syntax in Blade for the Form::open statement that allows the selected id from the drop-down list to be attached and sent to the controller. There MUST be a way to do this -- it's really a very commonly used technique in web programming. It's incredibly easy to do with old fashioned procedural code. But I'm completely stumped at the moment in trying to figure out how to do this in Laravel. I've posted both here and on Stack Overflow, so far with only a couple of responses, and problem not yet solved. Any other ideas? Thanks again!
I finally figured out the problem, and the solution:
The problem is that the form should (optimally) be sent as POST, and therefore not changed from the default value. BUT then the route has to be registered correctly as POST, and that's what I wasn't doing before. So,
dashboard.blade.php:
{{ Form::model(null, array('route' => array('lookupsociety'))) }}
{{ Form::select('value', $societies) }}
Route:
Route::post('lookupsociety', array('as'=>'lookupsociety', 'uses'=>'SocietiesController@lookupsociety'));
SocietiesController@lookupsociety:
public function lookupsociety()
{
$id = Input::get('value'); ... // proceed to do whatever is needed with $id value passed from the select list
}
It works perfectly! The key was changing the method in the route to Route::post() instead of Route::get().
I knew it had to be simple -- I simply hadn't stumbled on the solution before :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community