laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM
<?php namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB; class StudentController extends Controller
{
//DB facade原始SQL语句
public function test1()
{
$students = DB::select('select * from student');
//var_dump($students);
dd($students);
}
//查询构造器新增数据-增
public function query1()
{
//普通插入
// $bool = DB::table('student')->insert(
// ['name' => 'imooc', 'age'=> 20]
// );
// dd($bool); //返回自增长id
// $id = DB::table('student')->insertGetId(
// ['name' => 'cxll', 'age'=> 18]
// );
// var_dump($id);
//批量插入
$bool = DB::table('student')->insert([
['name'=>'name1', 'age' =>20],
['name'=>'name2', 'age' =>19]
]);
dd($bool); } //查询构造器更新数据-改
public function query2()
{
//返回影响行数
// $num = DB::table('student')
// ->where('id', 2)
// ->update(['age'=>30]);
// dd($num); //自增减 默认1
//$num = DB::table('student')->increment('age');
//$num = DB::table('student')->increment('age',3);
//$num = DB::table('student')->decrement('age',5); // $num = DB::table('student')
// ->where('id', 3)
// ->decrement('age');
//自增同时更新其它数据
$num = DB::table('student')
->where('id', 3)
->increment('age',2,['name'=> 'newname']); dd($num);
} //查询构造器删除数据-删
public function query3()
{
//返回操作影响的行数
// $num = DB::table('student')
// ->where('id',4)
// ->delete(); // $num = DB::table('student')
// ->where('id','>',2)
// ->delete(); // dd($num); //清空表 无返回值
DB::table('student')->truncate(); }
//查询构造器查询数据-查
public function query4(){ // $bool = DB::table('student') ->insert([
// ['id' => 1001, 'name' => 'name1', 'age' =>18],
// ['id' => 1002, 'name' => 'name2', 'age' =>19],
// ['id' => 1003, 'name' => 'name3', 'age' =>18],
// ['id' => 1004, 'name' => 'name4', 'age' =>21],
// ['id' => 1005, 'name' => 'name5', 'age' =>18]
// ]);
// dd($bool); //get()
//$students = DB::table('student')->get(); //first()
// $student = DB::table('student')
// ->orderBy('id', 'desc')
// ->first();
// dd($student); //where()
// $students = DB::table('student')
// ->where('id', '>=', 1002)
// ->get();
// $students = DB::table('student')
// ->whereRaw('id >= ? and age > ?', [1001, 18])
// ->get();
// dd($students); //pluck() 取字段
// $names = DB::table('student')
// ->pluck('name');
//指定下标为id
// $names = DB::table('student')
// ->pluck('name','id'); //lists() laravel5.5不支持lists了, 被pluck取代
//$names = DB::table('student')->lists('name','id'); //select() 指定查找
// $names = DB::table('student')
// ->select('id', 'name')
// ->get();
// dd($names); //chunk() 分段获取 需指定排序字段
echo '<pre>';
DB::table('student')->orderBy('id')
->chunk(2, function($students){
var_dump($students);
});
} //聚合函数
public function query5()
{
//$num = DB::table('student')->count();
//$max = DB::table('student')->max('age');
//$min = DB::table('student')->min('age');
//$avg = DB::table('student')->avg('age');
$sum = DB::table('student')->sum('age');
dd($sum);
}
//使用orm查询数据-查
public function orm1()
{
//all()
//$students = Student::all();
//find()
//$student = Student::find(1001);
//findOrFail()
//$student = Student::findOrFail(1001);
//dd($student);
//get()
//$students = Student::get();
//first()
// $student = Student::where('id', '>', '1001')
// ->orderBy('age', 'desc')
// ->first();
// dd($student);
//chunk()
// echo '<pre>';
// Student::chunk(2, function($students){
// var_dump($students);
// });
//聚合函数
//$num = Student::count();
$max = Student::where('id','>',1001)->max('age');
dd($max);
}
//使用orm新增数据-增
public function orm2()
{
//使用模型新增数据
// $student = new Student();
// $student->name = 'sean2';
// $student->age = 20;
// //存入数据库
// $bool = $student->save();
// dd($bool);
//$student = Student::find(1010);
//dd($student->created_at);
//将时间戳按格式输出
//echo date('Y-m-d H:i:s', 1464509164);
//使用模型的Create方法新增数据
// $student = Student::create(
// ['name' => 'imooc2', 'age' => 19]
// );
// dd($student);
//firstOrCreate() 查不到就新增至数据库
// $student = Student::firstOrCreate(
// ['name'=>'imooc3'],
// ['age'=>18]
// );
//firstOrNew() 查不到就创建实例,不主动插入数据库
$student = Student::firstOrNew(
['name'=>'imooc4'],
['age'=>18]
);
$student->save();
dd($student);
}
//使用orm更新数据-改
public function orm3()
{
//通过模型更新数据
// $student = Student::find(1014);
// $student->name = 'kitty';
// $bool = $student->save();
// dd($bool);
$num = Student::where('id', '>', 1012)->update(
['age'=>41]
);
dd($num);
}
//使用orm删除数据-删
public function orm4()
{
//通过模型删除
// $student = Student::find(1014);
// $bool = $student->delete();
// dd($bool);
//通过主键删除
// $num = Student::destroy(1013);
// $num = Student::destroy(1012,1013);
// $num = Student::destroy([1012,1013]);
// dd($num);
//删除指定条件的数据
$num = Student::where('id', '>', 1008)->delete();
dd($num);
}
}
Student 控制器类
laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM的更多相关文章
- [Laravel] 03 - DB facade, Query builder & Eloquent ORM
连接数据库 一.Outline 三种操作数据库的方式. 二.Facade(外观)模式 Ref: 解读Laravel,看PHP如何实现Facade? Facade本质上是一个“把工作推给别人做的”的类. ...
- Python版的数据库查询构造器、ORM及动态迁移数据表。
Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...
- Laravel—数据库操作与Eloquent模型使用总结
数据库操作 执行原生SQL //查询 $emp = DB::select('select * from employees where emp_no = 1'); $emp = DB::select( ...
- laravel 数据库操作
1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...
- [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)
简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...
- Laravel 数据库操作 Eloquent ORM
laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...
- laravel数据库操作
一.配置文件路径:/.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=test DB_USERNAME=root DB_P ...
- laravel 数据库操作(表、字段)
1)创建表(make:migration create),例如创建 articles php artisan make:migration create_articles_table --create ...
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
随机推荐
- Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
This lesson explains how to set rules for business classes and their properties. These rules are val ...
- 如何在CAD图纸中进行线性标注
在CAD中,都会在图纸中进行CAD标注,一般都是有CAD标注样式.CAD标注文字等.那其中有一个就是CAD线性标注?可以标注图纸间的距离?那如何在CAD图纸中进行线性标注呢?具体要怎么来进行操作?本篇 ...
- 解决npm下载慢的问题
方法一:使用淘宝定制的cnpm命令行工具替代默认安装npm npm install -g cnpm --registry=https://registry.npm.taobao.org 方法二:将np ...
- [转]UiPath教程:UiPath及其组件介绍
本文转自:http://www.rpa-cn.com/UiPathxuexirenzheng/UiPathzaixianxueyuan/2019-06-05/937.html 根据德勤2018年的调查 ...
- CamlQuery对SharePointOnline List 发起查询请求
最近的项目中遇到了一个需求,需要向SharePointList 查询Item是否存在,找到了CamlQuery这样一个方法,但是没有找到使用这个接口的频率限制说明文档,于是就有了这篇随笔. 新接触这个 ...
- 个人项目开源之c++基于epoll实现高并发游戏盒子(服务端+客户端)源代码
正在陆续开源自己的一些项目 此为c++实现高并发的游戏盒子,平台问题需要迁移重构,所以有一些遗留问题,客户端异常断开没有处理,会导致服务器崩溃,还有基于快写代码编程平台实现的小程序切换,线程读写缓存没 ...
- 小程序开发技术总结(wepy)
创建wepy项目 全局安装或更新WePY命令行工具:npm install wepy-cli -g 在开发目录中生成Demo开发项目:wepy new myproject , 1.7.0之后的版本使用 ...
- 渗透测试学习 三十一、MSF
术语 测试者利用系统程序或服务的漏洞进行攻击的一个过程——渗透攻击(exploit),攻击载荷(payload) 攻击者在目标系统上执行的一段代码,该代码具有反弹链接,创建用户.执行其他系统命令的功能 ...
- 算法问题实战策略 CHILDRENDAY
地址 https://algospot.com/judge/problem/read/CHILDRENDAY 题解 ac代码 #include <iostream> #include &l ...
- JavaScriptES6中Map与对象、数组,JSON之间的相互转换
JavaScriptES6中Map与对象.数组,JSON之间的相互转换 https://blog.csdn.net/c__dreamer/article/details/82183130