在 Laravel 中发布到相同的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20646142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:45:52  来源:igfitidea点击:

Posting to the same form in laravel

phplaravellaravel-4form-submit

提问by Gagan

I'm trying to build a search form in Laravel such that when a user presses the search button, all the contacts matching the search criteria are displayed on the same page.

我正在尝试在 Laravel 中构建一个搜索表单,以便当用户按下搜索按钮时,所有符合搜索条件的联系人都显示在同一页面上。

Below is my form:

下面是我的表格:

{{ Form::open(array('url' => '/directory', 'class' => 'form-horizontal')) }}
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">Last Name</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="lastname" placeholder="Enter Last Name">
        </div>
    </div>
    <div class="form-group ">
        <label for="phone" class="col-sm-2 control-label">Phone</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="phone" placeholder="Enter Phone number">
        </div>
    </div>
    <div class="form-group">
        <label for="inputEmail1" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-6">
            <input type="email" class="form-control" id="inputEmail1" placeholder="Enter Email - leave blank if you are not sure">
        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button type="submit" class="btn-u btn-u-green">Search</button>
        </div>
    </div>
    <div class="well">
        @foreach($users as $user)
            <li>{{$user->firstname}} {{$user->lastname}}</li>

        @endforeach
    </div>
{{ Form::close() }}

The screenshot below gives a pictorial view of my form:

下面的屏幕截图给出了我的表单的图形视图:

search page

搜索页面

My route is defined as follow:

我的路线定义如下:

Route::get('/directory', 'UsersController@filter');

Now, whenever I press the search button, I am getting a MethodNotFound exception.

现在,每当我按下搜索按钮时,我都会得到一个MethodNotFound exception.

What I really want to do is to show the search results below the search button.

我真正想做的是在搜索按钮下方显示搜索结果。

Edit ..

编辑 ..

Everything is working fine now except for one thing.

现在一切正常,除了一件事。

I'm displaying the data in my view (getting the list of all the users on my page which match the search criteria) but the for loop is not getting executed.

我在我的视图中显示数据(获取我的页面上与搜索条件匹配的所有用户的列表)但是 for 循环没有被执行。

<li>Count of users = {{ $users->count() }}</li>
<li>{{$users->first()->lastname}}</li>
@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

So: while I can see the count and the lastnameof the first user in the resultant array of records, the foreach loop is not getting executed.

所以:虽然我可以在结果记录数组中看到第一个用户的计数和姓氏,但 foreach 循环没有被执行。

What am I doing wrong?

我究竟做错了什么?

回答by Antonio Carlos Ribeiro

Laravel routes are bound to the form METHOD, so you need to create also a POST route:

Laravel 路由绑定到表单 METHOD,因此您还需要创建一个 POST 路由:

Route::post('/directory', 'UsersController@filter');

You also need to add names to your form fields:

您还需要在表单字段中添加名称:

name="lastname"

for

为了

<input type="text" name="lastname" class="form-control" id="lastname" placeholder="Enter Last Name">

And for all the others.

对于所有其他人。

A controller method to handle that query could look like this:

处理该查询的控制器方法可能如下所示:

<?php

class UsersController extends Controller {

    public function filter() 
    {
        $users = User::query();

        if (Input::has('lastname'))
        {
            $users->where('lastname', Input::get('lastname'))
        }

        if (Input::has('phone'))
        {
            $users->where('phone', Input::get('phone'))
        }

        return View::make('your.view')
                ->with('userCount', $users->count());
                ->with('users', $users->get());
    }

}

And in your view you can just:

在您看来,您可以:

<li>Count of users = {{$userCount}}</li>

@foreach ($users as $user)
    <li>
        {{ $user->id }} - {{$user->lastname}}
    </li>
@endforeach

回答by Anam

You have to tell Laravel that you want to use getmethod for submitting a form to the server. otherwise, Laravel will use postmethod by default.

你必须告诉 Laravel 你想使用get方法向服务器提交表单。否则,Laravel 将post默认使用方法。

Try the following:

请尝试以下操作:

{{ Form::open(array('url' => '/directory', 'method' => 'get', 'class' => 'form-horizontal')) }}

                                                        ^^^