点击分类无法显示全部关联会议:SQL列不存在错误排查
解决会议分类关联查询的SQL错误问题
看起来你的核心问题出在Eloquent关联查询的whereHas闭包写法错误,导致生成的SQL引用了不存在的字段,另外前端AJAX还有几处拼写小问题,我来一步步帮你修复:
1. 修复Controller中的关联查询逻辑
你当前的WhereHasCategory方法里,whereHas的闭包错误地引用了中间表字段,并且条件判断逻辑有误。正确的写法应该是针对关联的categories模型,匹配分类ID,或者明确指定中间表的字段:
修正后的Controller方法:
public function WhereHasCategory(Request $request, $id) { // 路由定义了{id}参数,直接作为方法参数接收更清晰,无需从$request取 $conferences = Conference::whereHas('categories', function ($query) use ($id) { // 闭包内的$query对应Category模型,直接匹配分类ID即可 $query->where('id', $id); })->get(); return response()->json($conferences); }
为什么之前的写法错了?
你之前写的$categories->where('category_conference.id',$request->id),Eloquent在处理whereHas('categories')时,闭包内的查询是针对categories表的,而非中间表。如果需要直接操作中间表字段,也可以用wherePivot方法:
$query->wherePivot('category_id', $id);
不过第一种直接匹配Category模型ID的写法更符合Eloquent的关联设计逻辑。
2. 修复前端AJAX的拼写错误
你的AJAX代码里有几处拼写失误,会导致成功回调无法正确渲染页面:
removeClass('ative')→ 应为removeClass('active')(少了字母c)$('#conferences').html(newConferenes)→ 变量名应为newConferences(少了字母c)- 给li加active类时也写错成了
ative,一并修正
修正后的AJAX代码片段:
$("a[name='category']").on('click', function(){ var category_id = $(this).attr("id"); $('.Categories__Menu li').removeClass('active'); // 修正拼写 $(this).parent('li').addClass('active'); // 修正拼写 $.ajax({ url: '{{ route('category.conferences',null) }}/' + category_id, type: 'GET', success:function(result){ $('#conferences').empty(); var newConferences=''; var placeholder = "{{route('conferences.show', ['id' => '1', 'slug' => 'demo-slug'])}}"; $.each(result, function(index, conference) { var url = placeholder.replace(1, conference.id).replace('demo-slug', conference.slug); newConferences += '<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">\n' + ' <div class="card box-shaddow">\n' + ' <img class="card-img-top" src='+ conference.image +' alt="Card image cap">\n' + ' <div class="card-body">\n' + ' <p class="font-size-sm"><i class="fa fa-calendar" aria-hidden="true"></i> '+conference.start_date+'</p>\n' + ' <h5 class="card-title h6 font-weight-bold text-heading-blue">'+conference.name+'</h5>\n' + ' <p class="card-text font-size-sm"><i class="fa fa-map-marker" aria-hidden="true"></i> '+conference.place+', '+conference.city+'</p>\n' + ' </div>\n' + ' <div class="card-footer d-flex justify-content-between align-items-center">\n' + ' <a href="' + url + '" class="btn btn-primary text-white">More</a>' + ' <span class="font-weight-bold font-size-sm text-heading-blue"> </span>\n'+ ' </div>\n' + ' </div></div>'; }); $('#conferences').html(newConferences); // 修正变量名拼写 }, error: function(error) { console.log(error.status) } }); });
3. 验证关联模型的中间表配置(可选)
虽然你说Category::find(1)->conferences能正常返回,但为了避免潜在的关联歧义,最好在模型关联方法里明确指定中间表和外键:
修正后的模型关联:
// Conference模型 class Conference extends Model{ public function categories(){ return $this->belongsToMany('App\Category', 'category_conference', 'conference_id', 'category_id'); } } // Category模型 class Category extends Model { public function conferences(){ return $this->belongsToMany('App\Conference', 'category_conference', 'category_id', 'conference_id'); } }
测试验证
做完以上修改后,点击"IT"分类(category_id=2),应该就能正确查询到关联的会议并渲染到页面上了。如果还有问题,可以:
- 直接访问
/conferences/where/category/2查看返回的JSON数据是否正确 - 检查浏览器控制台是否有其他JS错误
- 确认
category_conference表的关联数据是否准确
内容的提问来源于stack exchange,提问作者johnW
相关产品推荐
相关产品推荐

