1 回答
TA贡献1828条经验 获得超13个赞
明显的问题出在$materials->count() > 0if 语句中。
$materials 作为数组启动,并且->count()不存在于数组中。
更改if($materials->count() > 0)为if(count($materials) > 0),它应该可以工作
public function filterCategory(Request $request){
if($request->ajax()){
$query = $request->get('filter');
$materials = array();
if( $query != ''){
foreach ($query as $cat){
$materialsList = Material::where('category','=', $cat)->get();
array_push($materials,$materialsList);
}
}else{
$materialsList = Material::all();
}
if(count($materials) > 0){ //Changed
$output = "";
foreach ($materialsList as $material){
$output .= "<div class='col'>" .
"<div class='card mt-2'>" .
"<img class='shopImage' src='" . $material['image'] . "' alt='product'/>" .
"<div class='card-body'>" .
"<h5 class='card-title'>" . $material['name'] . "</h5>" .
"</div>" .
"<ul class='list-group list-group-flush'>" .
"<li class='list-group-item'>Quantidade:" . $material['amount'] . "</li>" .
"<li class='list-group-item'>Categoria:" . strtoupper($material['category'][0]).substr($material['category'],1) . "</li>" .
"<li class='list-group-item'><a href='" . route('edit_item',$material['id']) . "' class='btn btn-primary'>Editar</a></li>" .
"</ul>" .
"</div>" .
"</div>";
}
} else {
$output = 'Item com esse nome não encontrado!';
}
$data = $output;
echo json_encode($data);
}
}
添加回答
举报