字数:1920
作者:wookcool
阅读预计4分钟
之前介绍过自定义页面并实现跳转,但那都是静态的,没有与数据库进行任何信息交换。Laravel 内置的 Eloquent ORM为我们操作数据库提供了很大方便,要与数据库进行交互大致为以下流程:
一、建立模型
可以认为模型是数据库表的抽象。
运行命令:php artisan make:model Models/youxian,即可创建一个模型类
补充:
1、要使用先前已经存在的数据库表 baojie_tuidan_baojie_tuidans ,必须指定:protected $table = 'baojie_tuidan_baojie_tuidans';
2、Eloquent 默认表的主键为 id,你可以在模型类中定义一个 $primaryKey 来自定义;
3、Eloquent 默认 created_at 和 updated_at 已经存在于表中,如果不想 Laravel 自动管理“创建时间”、“修改时间”,在模型类中设置 $timestamps 属性为 false;
二、与数据库进行交互
之前的文章里创建了视图控制器(viewsController.php)来指向(return view....)需要显示的页面。这里我直接在视图控制器里面写。
首先 use AppModelsyouxian; 引用模型
在return view之前,查询数据( $gongdanhao 这些都是查询的参数):
$results=youxian::where('Work_order_no','like',"%{$gongdanhao}%") ->whereIn('Action_type',$actionType) ->where('Channel_name','like','%'.$channel.'%') ->where('The_theme','like','%'.$zhuti.'%') ->whereIn('Business_types',$yewu) ->where('The_user_account','like','%'.$zhanghao.'%') ->where('To_accept_the_person','like','%'.$createGongdanPeople.'%') ->whereIn('The_construction_way',$jianshe) ->where('note','like','%'.$GongdanBeizhu.'%') ->whereIn('county',$county) ->where($theTime) ->select('The_theme', 'To_accept_the_person', 'Channel_name','Business_types', 'The_construction_way','note', 'The_repair_order_at_time','The_single_time', 'The_construction_notes','USERID','USERID_password', 'Associated_broadband_account','With_d_team','Installed_staff_name' ) ->orderby('The_repair_ord er_at_time','desc') ->get();
补充:whereIn('county',$county),$county 其实是一个数组,类似 [ ['地区1'] ,['地区2'] ......]
如果ORM查询方式无法满足需求,还可以执行原生SQL语句:
无论哪一种查询,查询结果记得放到return view 里面给页面解析
三、页面上显示查询的结果
其实就是通过模板变量来解析查询结果,有关模板变量的用法请参考官方或其他教程
报结记录
总数:{{ $youxian_count }}
@isset($youxian_baojieTime_array) 日期范围:
@foreach($youxian_baojieTime_array as $youxian_baojieTime_array_i) {{ $youxian_baojieTime_array_i->日期 }} @endforeach @endisset
从数据库中删除、修改、添加与查询相差无几,请参考官方或其他教程