Php 带过滤器的Laravel搜索表单

Php 带过滤器的Laravel搜索表单,php,laravel,Php,Laravel,我想在Laravel中实现一个搜索表单,我在YouTube上观看了每一个视频,并尝试了许多教程,但我的代码不起作用;( 请帮我修复我的代码 谢谢你的帮助 文件web.php 在我的路由中有一个资源,其中包含客户机的CRUD,它还将有一个搜索,我希望通过名称、状态等实现过滤器 Route::resource('clients', 'ClientController'); Route::any('/search', 'ClientController@search')->name('sear

我想在Laravel中实现一个搜索表单,我在YouTube上观看了每一个视频,并尝试了许多教程,但我的代码不起作用;(

请帮我修复我的代码

谢谢你的帮助


文件web.php 在我的路由中有一个资源,其中包含客户机的CRUD,它还将有一个搜索,我希望通过名称、状态等实现过滤器

Route::resource('clients', 'ClientController');
Route::any('/search', 'ClientController@search')->name('search');

文件ClientController.php 函数搜索是一个试图实现搜索,但我知道是完全错误的,但我没有在谷歌上找到任何明确的教程,我知道如何正确地做

public function search(Request $request)
{
    $nome = $request->nome;
    $uf = $request->uf;
    $pesquisa = Client::where('nome', 'like', '%'.$nome.'%')
    ->orderBy('nome')
    ->paginate(5);

    return view('clients.index')
        ->with('pesquisa', $pesquisa)
        ->with('clients', $clients);
}

文件index.blade.php 这是我在前端的页面…有一个包含所有客户端的表,在上面我想实现一个按名称和状态筛选的搜索

@section('title', 'Clientes')

@section('content_header')
    <h1>
      Clientes
      &nbsp;&nbsp;
      <a href="{{ route('clients.create') }}" class="btn btn-sm btn-success">
        Novo Cliente
      </a>  
    </h1>  
@endsection

@section('content')
<div class="box">
  <div class="box-header">
    <nobr>
    <form action="/painel/search" method="POST" class="form form-inline" role="search">
      @csrf
      <div class="col-2">
        <input type="text" name="nome" class="form-control" placeholder="Nome">
        &nbsp;
        <input type="text" name="uf" class="form-control" placeholder="UF">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <button type="submit" class="btn btn-primary">Pesquisar</button>
       </div>
    </form>
    </nobr>
  </div>
</div>

</br>

<div class="card">
    <div class="card-body">
        <table class="table table-hover">
            <thead>
                  <tr>
                      <th>Nome</th>
                      <th>UF</th>
                      <th>Telefone</th>
                      <th>
                        <nobr>Ações</nobr>
                      </th>
                  </tr>
                </thead>
            <tbody>
                  @foreach($clients as $client)
                    <tr>
                      <td>{{$client->nome}}</td>
                      <td>{{$client->uf}}</td>
                      <td>{{$client->telefone}}</td>
                      <td>
                        <nobr>
                          <a href="{{ route('clients.edit', ['client' => $client->id]) }}" class="btn btn-sm btn-info">Editar</a>

                          <form class="d-inline" method="POST" 
                              action="{{ route('clients.destroy', ['client' => $client->id]) }}" 
                              onsubmit="return confirm('Tem certeza que deseja excluir o cliente?')">
                              @method('DELETE')
                              @csrf
                              <button class="btn btn-sm btn-danger">Excluir</button>
                          </form>

                        </nobr>
                      </td>
                    </tr>        
                  @endforeach

                </tbody>
        </table>
    </div>   
</div>

{{ $clients->links() }}

@endsection
@节(“标题”、“客户”)
@节(“内容\标题”)
客户
@端部
@节(“内容”)
@csrf
佩斯奎萨尔

诺姆 超滤 电传 Ações @foreach($clients作为$client) {{$client->nome} {{$client->uf} {{$client->telefone} @方法('DELETE') @csrf 排他性 @endforeach {{$clients->links()} @端部
您的控制器中没有定义
$clients
变量,因此将其包含在
返回中是没有意义的。此外,在您的视图中,您循环使用该变量,我认为如果您只需将保存查询结果的变量名称从
$pesquisa
更改为<代码>$clients
,一切正常:

public function search(Request $request)
{
    $nome = $request->nome;

    $clients = Client::where('nome', 'like', '%'.$nome.'%')
        ->orderBy('nome')
        ->paginate(5);

    return view('clients.index')
        ->with('clients', $clients);
}
此外,您的表单操作可能是错误的,请尝试使用路由名称而不是uri:

<form action="{{ route('search') }}" //...

谢谢你的帮助!!!=D我做了你告诉我的一切,但是当我输入名称并单击搜索按钮时,即使名称已存在,表也是空的。您在输入中输入的名称是什么?数据库中的名称是什么,在我的数据库中:nome在我的数据库中,所有字段都是大写的(大写),带有函数mb_strtoupper,我不知道这是否会影响……在这种情况下,您可以使用)
Client::where('nome','like','%'。strtoupper('nome.'%')/…
什么不起作用,有什么错误?