首页 > 解决方案 > 在 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');

请修改我的视图以通过复选框分别获取这两个结果。

标签: phpmysqllaravel

解决方案


有很多方法可以实现您正在尝试的目标。我展示的示例是使用 Jquery。

  1. 在您的表格中添加一个 ID,例如

    <table class="table" id="publication-detail"> <table class="table" id="research-detail">

  2. 将事件on click侦听器添加到您的复选框。当其中一个复选框被点击时,比如说发表,然后隐藏您的研究详细信息表。例如

    $(".checkbox-publication").click(function(){ $("#research-detail").css('display', 'none'); $("#publication-detail").css('display', 'block'); });

反之亦然。如果两者都单击,则显示两个表。

如果您想使用更高级和更高效的东西,请尝试 Ajax。

  1. https://api.jquery.com/click/
  2. https://api.jquery.com/css/
  3. https://api.jquery.com/jQuery.ajax/

推荐阅读