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
– Denis Rudnei de Souza
The view code is up there. I didn’t put it in full, There’s the part that I believe matters.
– Marcelo
In this case, the part where you call the variable customers would be the most important
– Denis Rudnei de Souza
I put the whole view.
– Marcelo
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
– Felipe Paetzold