我有一个显示某些类别的列表:
<ul class="Categories__Menu">
@foreach($categories->take(6) as $category)
<li class="active">
<a href="#" name="category" id="{{$category->id}}" href="#">{{$category->name}}</a>
</li>
@endforeach
<li><a data-toggle="modal" data-target=".bd-example-modal-lg" href="">More <i class="fa fa-caret-down" aria-hidden="true"></i></a></li>
</ul>
当用户点击某个类别时,会执行ajax reqquest以获取属于该被点击类别的会议。例如,当用户点击类别&#34; IT&#34;在数据库中有2个会议,分类为#34; IT&#34;而不是在网络选项卡中使用&#34;在视图中显示2个会议。 DD($会议);&#34;看来:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.conference_id' in 'where clause' (SQL: select * from conferences where exists (select * from categories where conferences.id = categories.conference_id and category_conference.id = 2))".
路线:
Route::get('conferences/where/category/{id}','ConferenceController@WhereHasCategory')->name('category.conferences');
ConferenceController方法:
public function WhereHasCategory(Request $request)
{
$conferences = Conference::whereHas('categories', function ($categories) use (&$request) {
$categories->where('category_conference.id',$request->id);
})->get();
//dd($conferences);
return response()->json($conferences);
}
的Ajax:
$("a[name='category']").on('click', function(){
var category_id = $(this).attr("id");
$('.Categories__Menu li').removeClass('ative');
$(this).parent('li').addClass('ative');
$.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(newConferenes);
},
error: function(error) {
console.log(error.status)
}
});
});
例如,在数据库中有5个会议,其中包含category_id&#34; 2&#34;。当点击id为2的类别时,结果就是问题只有一个结果。数据透视表category_conference具有:
id conference_id category_id
1 1 1
2 7 2
3 8 2
4 9 2
5 10 2
...
11 16 2
型号:
class Conference extends Model{
public function categories(){
return $this->belongsToMany('App\Category');
}
}
class Category extends Model
{
public function conferences(){
return $this->belongsToMany('App\Conference');
}
}
dd(Category :: find(1) - &gt;会议)显示:
Collection {#269
#items: array:2 [
0 => Conference {#273
#fillable: array:18 [
0 => "name"
...
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 1
"name" => "Conference title test"
...
]
#original: array:25 [
"id" => 1
"name" => "Conference title test"
...
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#262
+pivotParent: Category {#268
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [
"id" => 1
"name" => "Category1"
...
]
#original: array:5 [
"id" => 1
"name" => "Category1"
...
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"conferences" => Collection {#269}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
#foreignKey: "category_id"
#relatedKey: "confenrece_id"
#guarded: []
#connection: "mysql"
#table: "category_conference"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"category_id" => 1
"conference_id" => 1
]
#original: array:2 [
"category_id" => 1
"conference_id" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
1 => Conference {#270
#fillable: array:18 [
0 => "name"
1 => "description"
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 2
"name" => "conference test title 2"
...
]
#original: array:25 [
"id" => 2
"name" => "conference test title 2"
...
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#267
+pivotParent: Category {#268}
#foreignKey: "category_id"
#relatedKey: "conference_id"
#guarded: []
#connection: "mysql"
#table: "category_conference"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"category_id" => 1
"conference_id" => 2
]
#original: array:2 [
"category_id" => 1
"conference_id" => 2
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
}
答案 0 :(得分:2)
从类别中获取会议:
$conferences = Category::find($request->id)->conferences;
正如Farrukh Ayyaz所说,route model binding更优雅:
Route::get('conferences/where/category/{category}', ...
public function WhereHasCategory(Category $category)
{
return response()->json($category->conferences);
}