我有类别表和俱乐部表,两者之间的关系是
我想根据类别显示俱乐部,将通过下拉列表选择俱乐部,因此其他俱乐部将被隐藏。请参阅屏幕截图以了解更多
public function club()
{
$categories_name = Category::pluck('category_name','id');
$user_id = auth()->user()->id;
$user = User::find($user_id);
$data = array(
'user_clubs' => $user->clubs,
'categories_name' => $categories_name
);
return view('pages.dashboard.club_dashboard')->with($data);
}
<div class="form-group">
<strong>{{Form::label('categories_name', 'Select Category')}}</strong>
{{Form::select('categories_name', $categories_name, null, ['class' => 'form-control'], array('name'=>'categories_name[]'))}}
</div>
@if (count($user_clubs)>0)
<table class="table table-striped">
<tr>
<th><strong>Club Name</strong></th>
<th></th>
<th></th>
</tr>
@foreach ($user_clubs as $club)
<tr id="{{$club->category->id}}">
<th>{{$club->club_name}} | {{$club->category->category_name}} </th>
<td><a href="/clubs-soc/lists/{{$club->id}}/edit" class="btn btn-warning">Edit</a></td>
<td>
{!!Form::open(['action' => ['ClubsController@destroy', $club->id], 'method' => 'POST', 'class' => 'float-right'])!!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
@endforeach
</table>
@else
<p>You Have No posts</p>
@endif
3。类别模型
class Category extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function clubs(){
return $this->hasMany('App\Club');
}
}
4。俱乐部模型
class Club extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
}
5。我的脚本 我是根据朋友给出的答案写的这个脚本,仍然无法正常工作,请帮我找到:
<script>
$(document).ready(function(){
$("#categories_name").on("change", function(){
var category_id = $(this).val();
$("table tr").each(function(index){
if (index != 0) {
$row = $(this);
if ($row.attr('id')!=category_id) {
$row.hide();
}
else {
$row.show();
}
}
});
});
});
</script>