Simple search error in Laravel

0

I'm trying to perform a simple search. When you put dd($clientes) in the search method it is returning the search result correctly, but when returning to the view the error:

  

Trying to get property of non-object (View:   /var/www/html/neowix/resources/views/clients/index.blade.php)

View:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="panel panel-primary">
        <div class="panel-heading">Clientes</div>
        <div class="panel-body">
            <div class="row">
                <div class="col-lg-6">
                    <a class="btn btn-primary" href="/clientes/create" role="button">Incluir</a>
                </div><!-- /.col-lg-6 -->

                <div class="col-lg-6">
                    <form action="/clientes/search" method="get" class="form-inline">
                        <div class="form-group">
                            <input type="text" class="form-control" name="palavraChave" id="campo_busca" placeholder="Nome ou CNPJ">
                        </div>
                        <button type="submit" class="btn btn-default">Buscar</button>
                    </form>
                </div><!-- /.col-lg-6 -->

            </div><!-- /.row -->
        </div>
        <table class="table table-bordered">
            <tr>
                <th>CNPJ</th>
                <th>Razão Social</th>
                <th>Nome Fantasia</th>
                <th>Alterar</th>
                <th>Excluir</th>
            </tr>
            @foreach($clientes as $value)
                <tr>
                  <td class="cnpj">{{$value->cnpj}}</td>
                  <td>{{$value->razao_social}}</td>
                  <td>{{$value->nome_fantasia}}</td>
                  <td>
                    <a href="/clientes/edit/{{$value->id}}" class="glyphicon glyphicon-pencil" aria-hidden="true"></a>
                  </td>
                  <td>
                    <a href="/clientes/destroy/{{$value->id}}" class="glyphicon glyphicon-trash" aria-hidden="true"></a>
                  </td>
                </tr>
            @endforeach     
        </table>

        <nav aria-label="...">
          <ul class="pager">
            <li><a href="#">Anterior</a></li>
            <li><a href="#">Próxima</a></li>
          </ul>
        </nav>
    </div>
</div>
@endsection

@section('javascript')
<script>
$(document).ready(function(){
$('.cnpj').mask('00.000.000/0000-00');
});
</script>
@endsection

Controller:

public function search(Request $request)
{
    $palavraChave = $request->input('palavraChave');
    $clientes = DB::table('clientes')->where('razao_social', $palavraChave)->first();
    return view('/clientes/index', ['clientes' => $clientes]);
}
    
asked by anonymous 06.02.2017 / 21:30

3 answers

2

I was able to solve the problem by changing from first () to get () in the method:

public function search(Request $request)
{
$palavraChave = $request->input('palavraChave');
$clientes = DB::table('clientes')->where('razao_social', $palavraChave)->first();
return view('/clientes/index', ['clientes' => $clientes]);
}
    
08.02.2017 / 23:46
1

Try the following:

public function index()
{
    $clientes = new Cliente();
    return view('clientes.index', [
        "clientes" => $clientes->all()
    ]);
}
    
07.02.2017 / 19:03
0

Try to use:

$clientes = DB::table('clientes')->where('clientes.razao_social', $palavraChave)->first();

Then give dd () just below in $clientes :

dd($clientes);

and see what comes back to you ..

    
07.02.2017 / 05:25