Error in simple search in Laravel

Asked

Viewed 145 times

0

I’m trying to perform a simple search. By putting dd($clientes) in the search method it is returning the search result correctly, but when returning to the view of 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]);
}
  • What is your view code? I think you’re accessing the value of an array as if it were an object

  • The view code is up there. I didn’t put it in full, There’s the part that I believe matters.

  • In this case, the part where you call the variable customers would be the most important

  • 1

    I put the whole view.

  • Marcelo, I’m glad you solved your problem, but when you ask put only the code snippets referring to the problem, it is easier to identify a solution

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]);
}

1

Try to do the following:

public function index()
{
    $clientes = new Cliente();
    return view('clientes.index', [
        "clientes" => $clientes->all()
    ]);
}
  • Nothing happens even from the error. Just update the page.

  • @Marcelo add dd($clients->all()) in the method, see if you have records

  • Yes, it returns the two records that exist in the bank.

  • So the problem is in the view

  • Yes, but it returns the two records, and should return only what I did in the search. In the code of my method it is returning the record I made in the search, the problem is being this error when it would return to the view.

  • 1

    @Marcelo the problem may be that you are trying to redirect to a url instead of the view, try using the method return redirect('/clientes/index')->with('cliente', $cliente);, may work, but it’s not ideal.

  • It remains on the same page. I gave a dd($customers) and it is returning the correct record as said.

Show 3 more comments

0

Try to use:

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

and then give dd() right down on $clientes:

dd($clientes);

and see what comes back to you..

  • It returns: Undefined variable: palavraChave In case, in my code when giving the dd($clients) it returns the client who did the search, my problem is being send this result to the view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.