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