当控制器需要一个对象时,如何从选择列表传递id值到控制器? laravel-4 [英] How to pass id value from select list to controller when controller expects an object? laravel-4

查看:186
本文介绍了当控制器需要一个对象时,如何从选择列表传递id值到控制器? laravel-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在仪表板视图中构建了一个选择列表,其中列出了各种组件的ID和名称。数据被传递给使用传递的id来创建视图的控制器,以提取该id的正确组件数据。问题是,控制器被构造为期望从中获取id的对象,因此当我从列表中提交id时,我得到一个尝试获取非对象的属性错误。 (不管我是提交到路由还是直接到控制器;我得到相同的错误。)这里的代码:



PagesController

  public function showDashboard()
{
$ components = Component :: lists ('name','id');
return View :: make('dashboard',array(
'components'=> $ components,...
));
}

选择列表的源代码片段:

 < form method =GETaction =https:// ...accept-charset =UTF-8> 
< select id =idname =id>< option value =2>组件名称< / option> ...
/ pre>

组件模型:

  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 :: close()}}

用于其他目的和工作正常,例如,当我从一个URL传递一个特定的id,它接受该id没有错误。所以,我知道这应该是简单的涉及形式开放,但我不能弄清楚。如何解决这个问题?谢谢!

解决方案

我终于找出了问题和解决方案:



问题是,表单应该(最佳地)作为POST(不是GET)发送,因此不会从Blade提供的默认值更改。然后路由必须正确注册为POST,这就是我以前没有做的。因此,



dashboard.blade.php:

  {{Form :: model(null,array('route'=> array('lookupsociety'))}} 
{{Form :: select('value',$ societies)}} ...

路线:

  Route :: post('lookupsociety',array('as'=>'lookup society','uses'=>'SocietiesController @ lookupsociety')); 

SocietiesController @ lookupsociety:

  public function lookupsociety()
{
$ id = Input :: get('value'); ... //从选择列表传递的$ id值继续做任何需要
}


$ b b

它工作得很好!关键是改变路由到Route :: post()而不是Route :: get()的方法。



我知道它必须简单 - 我根本没有在解决方案之前绊脚石:)


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!

解决方案

I finally figured out the problem, and the solution:

The problem is that the form should (optimally) be sent as POST (not GET), and therefore not changed from the default value provided by Blade. 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'=>'lookup society', '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 :)

这篇关于当控制器需要一个对象时,如何从选择列表传递id值到控制器? laravel-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆