Lavarel:原生sql筛选条件查询以及模板分页写法

/**
     * 根据条件进行数据筛选
     * @param Request $request
     * @param $gender
     * @param $ispay
     * @param $testitem
     * @param $stageno
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getListByArgs(Request $request,$gender, $ispay, $testitem, $stageno)
    {
//        查询条件
        $wheres = '';
        if ($wheres != '' && $gender != '') {
            $wheres .= ' and gender like "%' . $gender . '%"';
        } elseif ($wheres == '' && $gender != '') {
            $wheres .= ' gender like "%' . $gender . '%"';
        }

        if ($wheres != '' && $ispay != "") {
            $wheres .= ' and ispay like "%' . $ispay . '%"';
        } elseif ($wheres == '' && $ispay != "") {
            $wheres .= ' ispay like "%' . $ispay . '%"';
        }

        if ($wheres != '' && $testitem != '') {
            $wheres .= ' and testitem like "%' . $testitem . '%"';
        } elseif ($wheres == '' && $testitem != '') {
            $wheres .= ' testitem like "%' . $testitem . '%"';
        }

        if ($wheres != '' && $stageno != '') {
            $wheres .= ' and stageno like "%' . $stageno . '%"';
        } elseif ($wheres == '' && $stageno != '') {
            $wheres .= ' stageno like "%' . $stageno . '%"';
        }

        if ($wheres == '') {
            $wheres = '1=1';
        }

        $rows = DB::table('tbl_kaosheng')
            ->select(DB::raw('*'))
            ->whereRaw($wheres)
            ->paginate(10);

        return $rows;
    }

查询全部数据、带分页查询

//**********************前台代码***************************
<div class="wrapper">
        <div class="container-fluid">
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="{{route('admin_index')}}">仪表盘</a></li>
                                <li class="breadcrumb-item active">公告列表</li>
                            </ol>
                        </div>
                        <h2>
                            公告管理
                            <span class="ml-4">
                                <a href="javascript:void(0)" class="btn btn-lg font-16 btn-primary addModal">
                                    <i class="mdi mdi-plus-circle-outline"></i> 发布公告
                                </a>
                            </span>
                        </h2>
                    </div>
                </div>
            </div>
            <!-- end page title -->

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <table class="table dt-responsive nowrap">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>标题</th>
{{--                                    <th>是否置顶</th>--}}
                                    <th>是否显示</th>
                                    <th>发布时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>


                                <tbody>
                                @if (count($Notices) !== 0)
                                @foreach ($Notices as $Notice)
                                    <tr>
                                        <td>
                                          {{$Notice->noticesort}}
                                        </td>
                                        <td>
                                            <a href="javascript:void(0)" data-guid="{{$Notice->notice_guid}}" class="show_notice" title="查看公告">{{ $Notice->title }}</a>
                                        </td>
                                        <td>
                                            @if($Notice->isshow === 1)
                                                显示
                                            @elseif($Notice->isshow === 0)
                                                不显示
                                            @else
                                                Undefined
                                            @endif
                                        </td>
                                        <td>{{ $Notice->creattime}}</td>
                                        <td>
                                            <a href="javascript:void(0)" data-guid="{{$Notice->notice_guid}}" class="article-operate edit_notice">编辑</a>
                                            <a href="javascript:void(0)" data-url="{{route('notice_updateOneField',[$Notice->notice_guid,'isshow',$Notice->isshow])}}"  class="article-operate notice_updateOneField">
                                                @if($Notice->isshow === 1)
                                                    设置不显示
                                                @else
                                                    设置显示
                                                @endif
                                            </a>
                                            <a data-guid="{{$Notice->notice_guid}}" href="javascript:void(0)" class="article-operate delnotice">删除</a>
                                        </td>
                                    </tr>
                                @endforeach
                                    @else
                                <tr>
                                    <td colspan="5" style="text-align: center"> 暂无公告记录</td>
                                </tr>
                                @endif
                                </tbody>
                            </table>

                        </div> <!-- end card body-->
                    </div> <!-- end card -->
                </div><!-- end col-->
            </div>
            <!-- end row-->


//分页模块
            <div style="height: 52px;">

                <ul class="pagination pagination-mydef" style="float: left">
                    @if($Notices->currentPage() === 1)
                        <li class="page-item disabled">
                            <a class="page-link" href="{{ $Notices->url(1) }}" tabindex="-1">首页</a>
                        </li>
                    @else
                        <li class="page-item">
                            <a class="page-link" href="{{ $Notices->url(1) }}" tabindex="-1">首页</a>
                        </li>
                    @endif
                    {{ $Notices->onEachSide(2)->links() }}
                    @if($Notices->currentPage() === $Notices->lastPage())
                        <li class="page-item disabled">
                            <a class="page-link" href="{{ $Notices->url($Notices->lastPage()) }}"
                               tabindex="-1">尾页</a>
                        </li>
                    @else
                        <li class="page-item">
                            <a class="page-link" href="{{ $Notices->url($Notices->lastPage()) }}"
                               tabindex="-1">尾页</a>
                        </li>
                    @endif
                </ul>

                <span class="item-num">
								<span class="item-num-text">共</span>
								<span class="item-num-text blue">{{ $Notices->lastPage() }}</span>
								<span class="item-num-text">页,</span>
								<span class="item-num-text blue">{{ $Notices->total() }}</span>
								<span class="item-num-text">条记录</span>
							</span>

            </div>
        </div>
    </div>


//**********************UIsever层逻辑***********************
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $obj= new NoticeBll();
        $Notices=$obj->indexWithPage(10);
        return view('admin.notice.notice_index',['Notices'=>$Notices]);
    }


//**********************后台DAL层逻辑**********************
    // 获取所有用户列表
    public function index(){
        $Kaoshengs=Kaosheng::all();
        return $Kaoshengs;
    }

    // 获取所有用户列表-带分页
    public function indexWithPage($num){
        $Notices=Kaosheng::orderBy('creattime', 'desc')->paginate($num);
        return $Notices;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值