Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉雷维尔。控制器未从窗体中获取值_Laravel - Fatal编程技术网

Laravel 拉雷维尔。控制器未从窗体中获取值

Laravel 拉雷维尔。控制器未从窗体中获取值,laravel,Laravel,控制器未从窗体获取数据。我意识到表单在默认情况下有一个Post方法,而路由使用Get,但是如果我更改了它,那么表单将不会显示表单字段。验证失败,因为required没有获得任何值,所以它返回到同一页。如果我删除了验证过滤器,那么它确实会转到结果页面,但它所做的只是显示表的所有内容,因为它没有从表单中获取任何参数。奇怪的是,在过去,它是有效的,但我一定是把代码的某些部分弄乱了,而现在它没有。为了节省空间,我省略了许多与问题无关的字段 表格中有三个相互依存的字段“国家”、“地区”和“城镇”,可以填写

控制器未从窗体获取数据。我意识到表单在默认情况下有一个Post方法,而路由使用Get,但是如果我更改了它,那么表单将不会显示表单字段。验证失败,因为required没有获得任何值,所以它返回到同一页。如果我删除了验证过滤器,那么它确实会转到结果页面,但它所做的只是显示表的所有内容,因为它没有从表单中获取任何参数。奇怪的是,在过去,它是有效的,但我一定是把代码的某些部分弄乱了,而现在它没有。为了节省空间,我省略了许多与问题无关的字段

表格中有三个相互依存的字段“国家”、“地区”和“城镇”,可以填写

表格:

控制器

public function findproperty(){

    /*IT REPEATS THE COUNTRY QUERY ABOVE BECAUSE IT IS GOING TO USE IT
     *ON THE RESULTS PAGE AND IT GIVES THE USER TO SELECT AGAIN OTHER COUNTRIES
     *WITHOUT HAVING TO RETURN TO THE FIRST PAST PAGE*/


    $countries = DB::table('properties')
        ->select('country')
        ->distinct()
        ->get();


     /*FIRST VALIDATE INPUT DATA*/

     $validator = Validator::make(Input::all(),

                array(

                  'country'      =>'required',
                  'regions'      =>'required',
                  'transaction'  =>'required',
                  'town'         =>'required'

                  ));

                if($validator->fails()){                   

                    return Redirect::route('showrealestate')
                    ->withErrors($validator)
                    ->withInput();
                  }

                else{

                    $country        = Input::get('country');
                    $region         = Input::get('regions');
                    $town           = Input::get('town');
                    $transaction    = Input::get('transaction');
                    $pricefrom      = Input::get('pricefrom');
                    $priceto        = Input::get('priceto');
                    $roomsfrom      = Input::get('roomsfrom');
                    $roomsto        = Input::get('roomsto');
                    $builtyear      = Input::get('builtyear');
                    $swimming       = Input::get('swimming');
                    $garden         = Input::get('garden');
                    $garage         = Input::get('garage');
                    $message        = Input::get('message');


                }

                $country = DB::table('countries')->where('id_pais', $country)->pluck('nombre_pais');
                $region = DB::table('regions')->where('id_region', $region)->pluck('nombre_region');
                $town = DB::table('cities')->where('id_localidad', $town)->pluck('nombre_localidad'); 

                $users = DB::table('users')

                ->join('properties', 'users.id', '=', 'properties.id_user_fk')
                ->select('users.email', 'properties.id_user_fk', 'properties.country', 'properties.region', 'properties.town',
                         'properties.price', 'properties.rooms','properties.m2','properties.swimming',
                         'properties.garden','properties.garage','properties.builtyear','properties.message',
                         'properties.pic1',
                         'properties.pic2', 'properties.pic3','properties.pic4','properties.pic5','properties.pic6');



                if (!empty($country)) {
                $users = $users->where('country', '=', $country);
                }
                if (!empty($region)) {                      
                $users =    $users->where('region', '=', $region);
                }

                if (!empty($town)) {
                $users = $users->where('town', '=', $town);
                }
                if (!empty($transaction)) {                      
                $users =    $users->where('transaction', '=', $transaction);
                }

                if (!empty($pricefrom)) {
               $users =  $users->where('price', '>', $pricefrom);
                }
                if (!empty($priceto)) {                      
                $users =    $users->where('price', '<', $priceto);
                }

                if (!empty($roomsfrom)) {
                $users = $users->where('rooms', '>', $roomsfrom);
                }
                if (!empty($roomsto)) {                      
                $users =    $users->where('rooms', '<', $roomsto);
                }

                if (!empty($builtyear)) {
                $users = $users->where('builtyear', '>', $builtyear);
                }
                if (!empty($swimming)) {                      
                $users =    $users->where('swimming', '=', $swimming);
                }

                if (!empty($garage)) {
                $users = $users->where('garage', '=', $garage);
                }
                if (!empty($garden)) {                      
                $users =    $users->where('garden', '=', $garden);
                }
                if (!empty($message)) {                      
                $users =    $users->where('message', '=', $message);
                }


                $users = $users->get();



                 return View::make('realestate.externa.listproperty', compact('users','countries'));

                }

post方法是必需的,否则Laravel将不会使用正确的数据将其重定向到正确的方法。它以前是怎么工作的?也许是运气好吧

Route::get('realestate/listproperty', array(

    'as' =>'sacapropiedades',            
    'uses' =>'countriesregionstownsController@findproperty'       

));

Route::post('realestate/listproperty', array(

    'as' =>'sacapropiedades',            
    'uses' =>'countriesregionstownsController@findproperty'       

));

那么您可能不需要在GET上进行验证:

if (Request::getMethod() == 'POST')
{
    $validator = Validator::...
} 
编辑:

对不起,我忽略了这个问题:

使用Laravel的FormBuilder类,而不是手动编写表单标记:

<?php Form::open(array('route' => 'sacapropiedades', 'class' => 'form-horizontal', 'id' => 'my_form', 'name' => 'my_form')); ?>

不同之处在于,它将为您添加方法,还将为您的表单添加csrf令牌。

非常感谢。我仍然看不到任何变化,但你已经指出了非常正确的方向,我应该为此努力。我给了你正确的答案,因为至少现在我更接近于理解问题的本质。你好,安东尼奥,它终于起作用了。我在表单中添加了一个method=post,否则它将无法获得它。然后我选择了你建议的第一个选项,一个用于get,一个用于post。糟糕的是,我没有意识到你没有使用Form::open。编辑以向您展示如何使用它。
Route::match(array('GET', 'POST'), 'realestate/listproperty', array(

    'as' =>'sacapropiedades',            
    'uses' =>'countriesregionstownsController@findproperty'       

));
Route::any('realestate/listproperty', array(

    'as' =>'sacapropiedades',            
    'uses' =>'countriesregionstownsController@findproperty'       

));
if (Request::getMethod() == 'POST')
{
    $validator = Validator::...
} 
<?php Form::open(array('route' => 'sacapropiedades', 'class' => 'form-horizontal', 'id' => 'my_form', 'name' => 'my_form')); ?>