php - 在 laravel 问题中通过复选框获取数据
问题描述
我正在尝试使用 Laravel 中的两个单独查询从两个不同的表中获取数据。但是我想用两个不同的复选框显示这两个表数据,例如,如果我想查看出版物详细信息或教育详细信息或两者,请帮助我应该如何处理我的视图。
我的代码显示在这里:
控制器代码是 ExampleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Auth;
class ExampleController extends Controller
{
public function index()
{
$result = DB::table('education')->get();
$data = DB::table('publications')->get();
return view('triel',compact('result','data'));
}
}
我的视图文件是 trail.blade.php 并给出了它的代码。
@extends('layouts.app')
@section('content')
<div class="container"><br>
<h1>Irfan Khan Triel Page</h1>
<div class="text text-success text-center">
PHD Research
</div>
<table class="table">
<tr>
<th>
PHD Research Area
</th>
<th>University</th>
<th>Country</th>
</tr>
@foreach($result as $value)
<tr>
<td>{{$value->research_area}}</td>
<td>{{$value->univ}}</td>
<td>{{$value->country}}</td>
</tr>
@endforeach
</table>
<div class="text text-success text-center">
Publications Detail
</div>
<table class="table">
<tr>
<th>
Title
</th>
<th>Status</th>
<th>Year</th>
</tr>
@foreach($data as $value)
<tr>
<td>{{$value->title}}</td>
<td>{{$value->status}}</td>
<td>{{$value->year}}</td>
</tr>
@endforeach
</table>
@endsection
我的路线文件在这里给出。
Route::get('triel','ExampleController@index');
请修改我的视图以通过复选框分别获取这两个结果。
解决方案
有很多方法可以实现您正在尝试的目标。我展示的示例是使用 Jquery。
在您的表格中添加一个 ID,例如
<table class="table" id="publication-detail"> <table class="table" id="research-detail">
将事件
on click
侦听器添加到您的复选框。当其中一个复选框被点击时,比如说发表,然后隐藏您的研究详细信息表。例如$(".checkbox-publication").click(function(){ $("#research-detail").css('display', 'none'); $("#publication-detail").css('display', 'block'); });
反之亦然。如果两者都单击,则显示两个表。
如果您想使用更高级和更高效的东西,请尝试 Ajax。
推荐阅读
- javascript - 脚本类型模块在注入 iframe 时不起作用
- javascript - 尝试执行随机命令而无需重新启动机器人
- angular - 如何有条件地更改 Angular 中垫子单元格的背景颜色?
- elasticsearch - 需要写 DSL 来检查这 4 个事件在 Elasticsearch 中是否发生在 1 秒内
- python - 提供 s3.upload_file() 的 url
- kubernetes - Kubernetes:无法将 int64 转换为字符串。Kubernetes 无法解释 helmchart values.yaml 文件中的整数值
- firebase - 将 Cron 作业设置为放大
- javascript - 计算按钮问题,html和javascript
- java - Apache Camel 中的类型转换器问题
- android - 如何将 Intent.ACTION_VIEW 用于文档 URI 的图像?