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