【发布时间】:2019-04-14 21:26:41
【问题描述】:
我正在制作一个 Web 应用程序,其中有两个表:类和部分。我的迁移如下所示。
类
Schema::create('classes', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
部分
Schema::create('sections', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('class_id')->unsigned();
$table->foreign('class_id')->references('id')->on('classes');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
在我的 Blade 文件中,我想显示所有类的按钮。当我单击该按钮时,它应该显示部分表中 class_id 属于所选按钮的所有部分。
我被语法卡住了。我是 Laravel 的新手,不知道如何为按钮添加响应。
我的部分控制器
public function index()
{
$sections = Section::all();
$class = Class::all();
$users = User::all();
return view('sections.index')->with('sections', $sections)->with('class', $class)->with('users', $users);
}
刀片
@foreach($classs as $cat)
<button class="btn btn-info">{{$cat->title}}</button>
//this shows all buttons with class name
@endforeach
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
如何在这些表格下获取仅选定类的所有部分?
这是建议的代码,我在其中进行了更改,单击按钮时我没有获取数据。请告诉我我该怎么办?我必须更换控制器吗
@foreach($classs as $cat)
<button class="btn btn-info class_btn" id="{{$cat->class_id}}">{{$cat->title}}</button>
<div class="box-body" style="display:none" id="table_{{$cat->class_id}}">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
@foreach($sections as $sec)
<tr>
<td>{{$sec->title}}</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('classses')->where('id',$sec->class_id)->value('title')}}
</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('users')->where('id',$sec->user_id)->value('name')}}
</td>
<td>
<button class="btn btn-info" data-mytitle="{{$sec->title}}" data-myclassid="{{$sec->class_id}}" data-myuserid="{{$sec->user_id}}" data-secid={{$sec->id}} data-toggle="modal" data-target="#edit">Edit</button>
/
<button class="btn btn-danger" data-secid={{$sec->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
$('.box-body').hide();
$('#table_'+$(this).attr('class_id')).show();
});
</script>
【问题讨论】:
标签: php laravel laravel-5 button eloquent