我有一个问题,当使用ajax搜索数据库时,我在控制台中收到一条未定义的消息,不确定在测试中它在哪里出错。我要实现的是获得与搜索查询匹配的结果,以下是我当前正在使用的内容。
我正在测试它时,它可以工作...它返回2个对象数组的集合...
public function getBusinesses(Request $request){
if($request->ajax()){
$businesses = Business::whereHas('address' , function($query) {
$query->where('area', 'LIKE','%'.request('location').'%');
})->whereHas('service', function($query) {
$query->where('service', request('keyword'));
})->whereHas('service.keywords', function($query){
$query->where('keyword', request('keyword'));
})->with(['ratings' => function($query){
}])->with(['address' => function($query){}])->select('id','name','business_description')->get();
return response()->json($businesses);
}
}
这是通话成功时的javascript
response.forEach(function(data){
$('#main-content').append(`<div class="section-title-dark"><p>Businesses in your area </p> </div><div class="row"><div class="col-lg-3 col-md-3 col-sm-3 col-3 item-mb"><div class="service-box1 bg-body text-center"><img src="img/service/service8.png" alt="service" class="img-fluid"><div style="font-weight:300; font-size:85%; padding:10px; flex:1">${data.ratings[0].comment}</div></div></div><div class="col-lg-9 col-md-9 col-sm-9 col-9 item-mb"><div class="service-box1 bg-body text-center"><p class="img-fluid business-name"><a href="/business/${data.name}/${data.id}"> ${data.name} </a></p><div class="rating-score pull-right" style="display: inline-block;background-color: #1ec4b4;color: #ffffff;padding: 5px;width: 35px; text-align: center;">4.9</div><div class="clearfix total-reviews text-small pull-right" style="padding-top:1%; padding-left:90%">26 reviews</div><p class="business-description mb-none" style="font-weight:300;font-size:85%;">${data.business_description}</p><div style="padding-left:82%"><a href="#" class="btn btn-default-outline btn-sm block" style=" background-color: #ffffff; border-color: #cccccc; color: #000000;">Request a Quote</a></div></div></div>`
);
console.log(data.id);
});
下面的laravel代码似乎有效,但是它返回的企业没有搜索查询中请求的地址,这不是我想要的。
if($request->ajax()){
$businesses = Business::whereHas('address' , function($query) {
$query->where('area', 'LIKE','%'.request('location').'%');
})->whereHas('service', function($query) {
$query->where('service', 'LIKE','%'.request('keyword').'%');
})->whereHas('service.keywords', function($query){
$query->where('keyword', 'LIKE','%'.request('keyword').'%');
})->with(['ratings' => function($query){
}])->with(['address' => function($query){}])->select('id','name','business_description')->get();
return response()->json($businesses);
}