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

本文介绍了在Laravel5.4中使用ORM模式进行数据库操作,包括创建SchoolRepository、School模型和SchoolController,实现了学校数据的增删改查功能,并结合搜索和分页功能。详细展示了如何处理多条件查询、文件上传以及数据统计等操作。
摘要由CSDN通过智能技术生成

表名: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(&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值