-1
I’m having trouble filtering with two different models. The problem is that there is no relationship between the tables.
That’s right =/
The modeling was done by someone else, who said that it would not be possible to do the relationship and that I would have to do with what I have. Thus, in some modules I needed to make some queries and more queries to remedy this and make some data available to the user.
I have a view (oracle) of tools
And I have an order table, where I register the inventory_item_id and the part_number of the requested tool
<!-- Filtro -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Filtro</h6>
</div>
<div class="card-body">
<form action="/historico" method="get">
<div class="row">
<div class="col-md-2 px-1">
<label for="estado">Estado</label>
<select class="form-control" name="estado">
<option value="" selected disabled>Selecione um status</option>
<option value="Aberto" >Aberto</option>
<option value="Submetido" >Submetido</option>
<option value="Entregue" >Entregue</option>
<option value="Cancelado" >Cancelado</option>
</select>
</div>
<div class="col-md-2 px-1">
<label for="data">Data</label>
<input type="date" class="form-control" value="" name="data" >
</div>
<div class="col-md-3 px-1">
<label for="item">Item</label>
<input type="text" class="form-control" value="" name="item" >
</div>
<div class="col-md-4 px-1">
<label for="descricao">Descrição da ferramenta</label>
<input type="text" class="form-control" value="" name="descricao" >
</div>
<div class="col-md-1 align-self-end px-1 button">
<button type="submit" class="btn btn-secondary"><i class="fa fa-search fa-1x"></i></button>
</div>
</div>
</form>
</div>
</div>
And my controller:
$pedidos = DetalhePedido::when(Request::input('estado'),function($query){
$query->where('estado_linha',Request::input('estado'));
})
->when(Request::input('data'),function($query){
$query->where('creation_date', 'like', '%' . strtoupper(Request::input('data')) . '%');
})
->when(Request::input('item'),function($query){
$query->where('item', 'like', '%' . strtoupper(Request::input('item')) . '%');
})
->when(Request::input('descricao'),function($query){
$query->where('description','like', '%' . strtoupper(Request::input('descricao')) . '%');
})->paginate(10);
$estado = Request::input('estado');
$data = Request::input('data');
$item = Request::input('item');
$descricao = Request::input('descricao');
return view('historico', compact('pedidos', 'estado', 'data', 'item', 'descricao'));
Filtering by State and for Date works normally. The problem is that they also want filter by Item and Description, which are oracle view columns and which have no relationship.
Is there any way to fix it?
I see that your code can get smaller, and why did the guy say you can’t relate to him being dumb or what? Just can’t relate if the bank isn’t relational if it’s my friend, can send the guy back to algorithms because he’s not fit for the market
– Lucas Antonio
It is. It gave me a lot of work, but I managed to solve.
– Thiago Maciel
Which version of Laravel?
– Lucas Antonio
Oops, Lucas. It’s 5.8
– Thiago Maciel
Improve this code to spend 4 lines that your code in this version
– Lucas Antonio
What is your suggestion?
– Thiago Maciel
I will redo your code in today’s mode and you evaluates can be?
– Lucas Antonio
Yes yes, of course!!
– Thiago Maciel