I created a filter that returns data from my search through JavaScript, this filter have model, description, status, distributor, etc. I need to make a search field in a search return a category related to the product, but when I run my code, it returns nothing.
ProdutosController:
public function search(Request $request)
{
$product = Produtos::query();
$categoria = Categoria::query();
// $model = $request->model;
// $status = $request->status;
// $distributor = $request->distributor;
if ($request->isMethod('post'))
{
$data = json_decode($request->search_data);
$descricao;
$model;
$status;
$distributor;
$stock;
$image;
$categoria;
$descricao = $data->descricao;
$model = $data->model;
$status = $data->status;
$distributor = $data->distributor;
$stock = $data->stock;
$image = $data->image;
$categoria = $data->categoria;
session(['descricao' => $descricao]);
session(['model' => $model]);
session(['status' => $status]);
session(['distributor' => $distributor]);
session(['stock' => $stock]);
session(['image' => $image]);
session(['categoria' => $categoria]);
}
if (strlen(session('descricao')) > 0)
{
$product_ids = ProdutosDescricao::where('erp_name', 'LIKE', '%' . session('descricao') . '%')->get();
$ids = [];
foreach ($product_ids as $product_data)
{
$ids[] = $product_data->erp_productid;
}
$product = $product->whereIn('erp_productid', $ids);
}
if (strlen(session('model')) > 0)
{
$product = $product->where('erp_model', 'LIKE', '%' . session('model') . '%');
}
if (strlen(session('status')) > 0)
{
$product = $product->where('erp_status', session('status'));
}
if (strlen(session('distributor')) > 0)
{
$product = $product->where('erp_distributor', session('distributor'));
}
if (strlen(session('stock')) > 0)
{
if (intval(session('stock')) == 1)
{
$product = $product->where('erp_quantity', '>', 0);
} else
{
$product = $product->where('erp_quantity', '=', 0);
}
}
if (strlen(session('image')) > 0)
{
$URL_IMAGE = "http://comercio-urbano.s3-website-sa-east-1.amazonaws.com/produtos/marcas/nao_disponivel.jpg";
if (intval(session('image')) == 0)
{
$product_ids = ProdutoImage::where('erp_image', '=', $URL_IMAGE)->get();
$ids = [];
foreach ($product_ids as $product_data)
{
$ids[] = $product_data->erp_productid;
}
$product = $product->whereIn('erp_productid', $ids);
}
}
if (strlen(session('categoria')) > 0)
{
$categoria = $categoria->where('erp_category', session('categoria'));
}
$content = $product->paginate(30);
if ($request->isMethod('post'))
{
return view('products.product-table')->with('produtos', $content)->with('categoria', $categoria);
} else
{
return view('products.index')->with('produtos', $content)->with('categoria', $categoria);
}
}
ProductSearch.js
$(document).ready(function() {
$(document).on('blur', '#model', function() {
var model = $('#model').val();
searchRequest['model'] = model;
doSearch();
});
$(document).on('blur', '#descricao', function() {
var descricao = $('#descricao').val();
searchRequest['descricao'] = descricao;
doSearch();
});
$(document).on('blur', '#status', function() {
var status = $('#status').val();
searchRequest['status'] = status;
doSearch();
});
$(document).on('blur', '#distributor', function() {
var distributor = $('#distributor').val();
searchRequest['distributor'] = distributor;
doSearch();
});
$(document).on('blur', '#stock', function() {
var stock = $('#stock').val();
searchRequest['stock'] = stock;
doSearch();
});
$(document).on('blur', '#image', function() {
var image = $('#image').val();
searchRequest['image'] = image;
doSearch();
});
$(document).on('blur', '#categoria', function(){
var categoria = $('#categoria').val();
searchRequest['categoria'] = categoria;
doSearch();
});
});
$(document).on('click', '#clean-form-button', function() {
$('#descricao').val("");
$('#model').val("");
$('#distributor').val("");
$('#status').val("");
$('#stock').val("");
$('#categoria').val("");
searchRequest = {
'descricao': '',
'model': '',
'distributor': '',
'status': '',
'stock': '',
'image': '',
'categoria':'',
};
doSearch();
});
function IsEmpty(str)
{
if ( typeof str == 'undefined' || !str || str.length === 0 || str === "" || !/[^\s]/.test(str) || /^\s*$/.test(str) || str.replace(/\s/g, "") === "" )
{
return true;
}
else
{
return false;
}
}
function doSearch() {
$.post(postSearch, {
'search_data': JSON.stringify(searchRequest),
'_token': $('meta[name=csrf-token]').attr('content'),
}
, function(data) {
$('#product-table').html(data);
});
}
$("#model").html('<img src="https://mir-s3-cdn-cf.behance.net/project_modules/disp/1f430a36197347.57135ca19bbf5.gif" alt="Wait" />');
$('#message').load('index.php?pg=ProductSearch', null, function() {
$("#model").html('');
});
$body = $("body");
$(document).on({
ajaxStart: function() { $body.addClass("loading"); },
ajaxStop: function() { $body.removeClass("loading"); }
});
Products.index
<select class="selectpicker" data-live-search="true" id="erp_categoryid" name="erp_categoryid">
@foreach($categorias as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}" @if(session('categoria') == "{{$categoria->erp_categoryid}}") selected @endif>{{$categoria->erp_name}}</option>
@endforeach
</select>
product-table.blade.php
<td>@foreach($produto->categoria as $categoria)
{{$categoria->erp_name}}
@endforeach
</td>
Model
public function categoria()
{
return $this->belongsToMany('App\Models\Categoria', 'erp_product_category', 'erp_productid', 'erp_categoryid');
}
Any suggestion?