laravel5.4中增删改查+搜索分页(运用ORM模式所做)。

2024-04-03 17:08

本文主要是介绍laravel5.4中增删改查+搜索分页(运用ORM模式所做)。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

表名:3ls_school;

控制器名 :SchoolController.php

 模型层:School.php

工厂:SchoolRepository.php

第一步:建立学校工厂在/core/Repositories里面建SchoolRepository.php

内容为:

<?php 


namespace Core\Repositories;


use Core\Models\School; 
use Core\Repositories\SchoolRepository;


class SchoolRepository extends EloquentRepository

    public function __construct(School $model)
    {
        parent::__construct($model);
    }


    //add
    public function save($data)
    {
        $this->model->setRawAttributes($data);


        if ($this->model->save()) {
            return true;
       } else{
            return false;
       }
    }


    //select
    public function getList($perPage = 10, $where=[], $trash=0)
    {   
        
        $query = $this->model->whereNested(function ($q) use ($where) {
                foreach ($where as $key => $value) {
                    $q->where($value[0], $value[1], $value[2]);
                }
            });
        if ( $trash == 1 ){ 
            $query->onlyTrashed();
        }


        return $query->orderBy('id', 'ASC')->paginate($perPage);
    }


    //删除
    public function delete($id)
    {
        return $this->model->find($id)->delete();
    }


    /**
     * Find a single entity
     *
     * composite primary key array
     *  - MemberOption::scopeCompositeKey
     *
     * @param $id
     * @param array $with
     * @return Illuminate\Database\Eloquent\Model
     */
    //查询单条信息
    public function find($id, array $with = [])
    {
        $entity = $this->make($with);


        if (is_array($id)) {
            $model = $entity->compositeKey($id)->first();
        } else {
            $model = $this->model->find($id);
        }
       return $model;
    }


    public function findByField($field, $value, $columns = ['*'])
    {
        return $this->model->where($field, '=', $value)->first($columns);
    }


     /**
     * 根据条件查询单条记录
     * @param  array $filters [description]
     * @param  array $columns [description]
     * @return [type]          [description]
     */
    public function findWhere($filters = [], $columns = ['*'])
    {
        return $this->model->whereNested(function ($query) use ($filters) {
            foreach ($filters as $key => $value) {
                $query->where($value[0], $value[1], $value[2]);
            }
        })->first($columns);
    }


    //修改学校信息
    public function update($id, array $input)
    {
        return $this->model->where('id', '=', $id)->update($input);
    }
    
    //统计个数 
     public function count(array $where = [])
    {
        return $this->model->whereNested(function ($query) use ($where) {
            foreach ($where as $field => $value) {
                if (is_array($value)) {
                    list($condition, $val) = $value;
                    $query->where($field, $condition, $val);
                } else {
                    $query->where($field, '=', $value);
                }
            }
        })->count();
    }
       
}

第二步:建立模型层在在/core/Models里面建School.php

内容为:

<?php
namespace Core\Models;


use Illuminate\Database\Eloquent\Model;


class School extends Model
{
    protected $table = '3ls_school';
    public $timestamps = false;


    
}


第三步:建立模型层在在/app/Http/Controllers/里面建SchoolController.php

内容为:

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Core\Repositories\SchoolRepository;


class SchoolController extends BackendController
{
    private $schools;


    public function __construct(SchoolRepository $schools)
    {
        parent::__construct();
        $this->schools = $schools;
    }


    //学校列表
    public function index(Request $request)
    {   
        $where=[];
        //判断是否接过学段值
        if ($request->get("period")!="") {
            $where[] = ['3ls_school.period', '=', $request->get("period")];
        }
        //判断是否接过性质值
        if ($request->get("snature")!="") {
           $where[] = ['3ls_school.snature', '=', $request->get("snature")];
        }
        //判断是否接过等级值
        if ($request->get("slevel")!="") {
           $where[] = ['3ls_school.slevel', '=', $request->get("slevel")];
        }
        //判断所选择的是省份还是市级
        $area=$request->get("area");
        if ($area!="") {
          if ($area=="province") {
              $where[] = ['3ls_school.province', '=', $request->get("search")];
          }else{
              $where[] = ['3ls_school.city', '=', $request->get("search")];
          }
        }
        //dd($where);
        if (!empty($where)) {//带有搜索条件的查询
           $perpage = $request->input('perpage', 10);
           $this->_view['lists'] = $this->schools->getList($perpage,$where);//所查询的结果集
           //dd(\DB::getQuerylog());
           $this->_view['perpage'] = $perpage;//每页显示条数
        }else{
           $perpage = $request->input('perpage', 10);
           $this->_view['lists'] = $this->schools->getList($perpage);//所查询的结果集
           $this->_view['perpage'] = $perpage;//每页显示条数
        }
        return view('school.index', $this->_view);
    }


    //学校添加表单页面
    public function create()
    {
      return view('school.create', $this->_view);
    }


    //添加学校
    public function store(Request $request)
    {
        $data['name'] = $request->input('name');
        $data['province'] = $request->input('s_province');
        $data['city'] = $request->input('s_city');
        $data['town'] = $request->input('s_county');
        $data['period'] = $request->input('period');
        $data['snature'] = $request->input(&

这篇关于laravel5.4中增删改查+搜索分页(运用ORM模式所做)。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/873486

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

[MySQL表的增删改查-进阶]

🌈个人主页:努力学编程’ ⛅个人推荐: c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构,刷题刻不容缓:点击一起刷题 🌙心灵鸡汤:总有人要赢,为什么不能是我呢 💻💻💻数据库约束 🔭🔭🔭约束类型 not null: 指示某列不能存储 NULL 值unique: 保证某列的每行必须有唯一的值default: 规定没有给列赋值时的默认值.primary key:

poj 2431 poj 3253 优先队列的运用

poj 2431: 题意: 一条路起点为0, 终点为l。 卡车初始时在0点,并且有p升油,假设油箱无限大。 给n个加油站,每个加油站距离终点 l 距离为 x[i],可以加的油量为fuel[i]。 问最少加几次油可以到达终点,若不能到达,输出-1。 解析: 《挑战程序设计竞赛》: “在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一

hdu 4517 floyd+记忆化搜索

题意: 有n(100)个景点,m(1000)条路,时间限制为t(300),起点s,终点e。 访问每个景点需要时间cost_i,每个景点的访问价值为value_i。 点与点之间行走需要花费的时间为g[ i ] [ j ] 。注意点间可能有多条边。 走到一个点时可以选择访问或者不访问,并且当前点的访问价值应该严格大于前一个访问的点。 现在求,从起点出发,到达终点,在时间限制内,能得到的最大

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

hdu4277搜索

给你n个有长度的线段,问如果用上所有的线段来拼1个三角形,最多能拼出多少种不同的? import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序