在laravel中如何像搜索数据一样过滤数据?

问题描述 投票:0回答:2

laravel im刚开始做Questionbank项目时,我是如何使用下拉选择框通过多个过滤器搜索表的?例如按产品价格,产品规格和产品公司过滤并搜索数据请帮我在此先感谢

enter image description here

此查看表

<div class="container">
    <div align="right">

     <form  method="post" action="{{ url('topic/auto-search') }}">
        <div class="" align="left">
            <select class="col-md-3" id='sel_topic' name='sel_topic'>
       <option value='0'>-- Select Topic --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->topic_name }}</option>
       @endforeach
   </select><span>
       <select class=" col-md-3" id='sel_stand' name='sel_stand'>
       <option value='0'>-- Select Standard --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->standard_name }}</option>
       @endforeach
   </select>
     <select class="col-md-3" id='sel_sub' name='sel_sub'>
       <option value='0'>-- Select Subject --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->subject_name }}</option>
       @endforeach
   </select>
    </span>
        {{csrf_field()}}

             <input type="text"  name="search"  id="search">
            <button type="submit" class="btn btn-outline-secondary" name="searchbutton"><i class="fa fa-search"></i></button>&nbsp;&nbsp;


    <a class="btn btn-small btn-primary pull-right" style="margin-top: 5px;" href="{{ URL::to('topic/create') }}"><i class="fa fa-plus"></i>&nbsp;ADD</a>
</div>
</form>
</div>
<div class="">
    <br><br>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <td>ID</td>
            <td>Topic Name</td>
            <td>Standard</td>
            <td>Subject</td>
            <td>Actions</td>
        </tr>
    </thead>
    <tbody>

和我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\TopicRequest;
use App\Topic;
use App\Standard;
use App\Subject;
use DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;

class TopicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         $topics = Topic::leftJoin('standards','topics.standard_id', '=', 'standards.id')->
                  leftJoin('subjects','topics.subject_id', '=', 'subjects.id')
        ->select('topics.*' ,'standards.standard_name','subjects.subject_name')
        ->orderBy('id', 'ASC')
        ->paginate(10);
        return view('topic.index', compact('topics'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $standards = Standard::select('id','standard_name')->paginate(10);
        $subjects = Subject::select('id','subject_name')->paginate(10);
        return view('topic.create', compact('standards','subjects'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name'    =>  'required',
            'standard_id' => 'required',
            'subject_id' => 'required',
        ]);
        $topic = new Topic([
            'topic_name'    =>  $request->get('name'),
            'standard_id'    =>  $request->get('standard_id'),
            'subject_id'    =>  $request->get('subject_id'),
        ]);
        $topic->save();
        return redirect()->route('topic.index')->with('success', 'Data Added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $topic = Topic::find($id);
        $standards = Standard::select('id','standard_name')->paginate(10);
        $subjects = Subject::select('id','subject_name')->paginate(10);
        return view('topic.edit', compact('topic', 'id','standards','subjects'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $topic = Topic::find($id);
        $topic->topic_name = $request->get('name');
        $topic->standard_id = $request->get('standard_id');
        $topic->subject_id = $request->get('subject_id');
        $topic->save();
        return redirect()->route('topic.index')->with('success', 'Data Updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $topic = Topic::find($id);
        $topic->delete();
        return redirect()->route('topic.index')->with('success', 'Data Deleted');
    }
}
laravel search filter
2个回答
0
投票

使用数据表..并使用此

<select data-column="3"  class="filter-select">
         <option value='0'>-- Select Subject --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->subject_name }}</option>
       @endforeach
 </select>

并将其粘贴到您的文件中

 $(document).ready(function () {
      var table = $('#myTable').DataTable({
            "pagingType": "full_numbers"
        });

           $('.filter-select').change(function ( ){
            table.column( $(this).data('column') )
            .search( $(this).val() )
            .draw();
        });

    });

在表标签中使用此ID

<table id="myTable" class="table table-hover">

0
投票
public function index() 
{

  $params = $this->getAllParams();
  $topic = Topic::leftJoin('standards','topics.standard_id', '=', 'standards.id')
               ->leftJoin('subjects','topics.subject_id', '=', 'subjects.id')
               ->select('topics.*' ,'standards.standard_name','subjects.subject_name')
               ->orderBy('id', 'ASC');
  if (!empty($params['search']) || !is_null($params['search'])) {
    $topic->where("standards.standard_name", "like", "%".$params['search']."%");
  }
  .....

  $topics = $topic->paginate(10);

  return view("topic.index", compact("topics"));

}

public function getAllParams()
{
  $params['sel_topic'] = request()->get("sel_topic") ?: '';
  $params['sel_stand'] = request()->get('sel_stand') ?: '';
  $params['sel_sub'] = request()->get('sel-sub') ?: '';
  $params['search'] = reqeust()->get('search') ?: '';

  return $params;
}

您的视图

<div class="container">
    <div align="right">

     <form  method="get" action="{{ url('topic/auto-search') }}">
        <div class="" align="left">
            <select class="col-md-3" id='sel_topic' name='sel_topic'>
       <option value='0'>-- Select Topic --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->topic_name }}</option>
       @endforeach
   </select><span>
       <select class=" col-md-3" id='sel_stand' name='sel_stand'>
       <option value='0'>-- Select Standard --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->standard_name }}</option>
       @endforeach
   </select>
     <select class="col-md-3" id='sel_sub' name='sel_sub'>
       <option value='0'>-- Select Subject --</option>
       @foreach($topics as $topic)
         <option value='{{ $topic->id }}'>{{ $topic->subject_name }}</option>
       @endforeach
   </select>
    </span>


             <input type="text"  name="search"  id="search">
            <button type="submit" class="btn btn-outline-secondary" name="searchbutton"><i class="fa fa-search"></i></button>&nbsp;&nbsp;


    <a class="btn btn-small btn-primary pull-right" style="margin-top: 5px;" href="{{ URL::to('topic/create') }}"><i class="fa fa-plus"></i>&nbsp;ADD</a>
</div>
</form>
</div>
<div class="">
    <br><br>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <td>ID</td>
            <td>Topic Name</td>
            <td>Standard</td>
            <td>Subject</td>
            <td>Actions</td>
        </tr>
    </thead>
    <tbody>

© www.soinside.com 2019 - 2024. All rights reserved.