<?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的更多相关文章

  1. [Laravel] 03 - DB facade, Query builder & Eloquent ORM

    连接数据库 一.Outline 三种操作数据库的方式. 二.Facade(外观)模式 Ref: 解读Laravel,看PHP如何实现Facade? Facade本质上是一个“把工作推给别人做的”的类. ...

  2. Python版的数据库查询构造器、ORM及动态迁移数据表。

    Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...

  3. Laravel—数据库操作与Eloquent模型使用总结

    数据库操作 执行原生SQL //查询 $emp = DB::select('select * from employees where emp_no = 1'); $emp = DB::select( ...

  4. laravel 数据库操作

    1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...

  5. [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)

    简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...

  6. Laravel 数据库操作 Eloquent ORM

    laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...

  7. laravel数据库操作

    一.配置文件路径:/.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=test DB_USERNAME=root DB_P ...

  8. laravel 数据库操作(表、字段)

    1)创建表(make:migration create),例如创建 articles php artisan make:migration create_articles_table --create ...

  9. yii学习笔记(7),数据库操作,联表查询

    在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...

随机推荐

  1. 本地SQL Server数据库提示网络问题无法连接

    运行程序时发现本地SQLserver数据库无法连接,提示信息为:在与SQL Server 建立连接时出现与网络相关的或特定与实例的错误.未能找到或无法访问服务器.请验证实例名称是否正确并且SQL Se ...

  2. Evaluation Warning : The document was created with Spire.PDF for .NET.

    由于使用  Spire.Pdf 生成的书签带有 Evaluation Warning : The document was created with Spire.PDF for .NET. 字样 但是 ...

  3. IT 常用的网址

    IT 常用的网址 将图片转换成网络图片的网址:https://sm.ms/ 生成 ico 图标: http://www.bitbug.net/ 动画特效: https://daneden.github ...

  4. 记一次mq无法正常生产消息的事故排查过程

    早上上班后得知,服务费未同步到代理商系统.查看draft_server系统生产环境的log,显示在往RabbitMQ推数据时出现异常:no route to host. 2019-07-29 01:3 ...

  5. Spark中持久化和序列化学习

    一.cache和persisit的对比 -rw-r--r--@ 1 hadoop staff 68M 5 17 07:04 access.log    cache/persitence是 laz ...

  6. sqliteman

    2.安装文件 采用源码方式安装 可用下面地址自行下载 https://sourceforge.net/projects/sqliteman/files/sqliteman/1.2.2/ 3.安装 1) ...

  7. txt换行追加写入

    with open(negative_txt, 'a') as f: patch_name1 = patch_name + '\n' f.write(patch_name1)

  8. QT新建空白项目-添加QT设计师界面类时出现的各种库无法导入识别

    按照教材上先新建一个空的项目--添加Qt设计师界面类时 出现各种 库无法识别 解决方法: 在 .pro文件中加入一行 QT += widgets 去构建中先执行 qmake 然后再构建一下  ok了 ...

  9. React: 认识React

    一.简介 React-Native是Facebook开源的跨平台框架,用于实现前端和原生进行混合开发.React-Native开发可以很好的使用原生UI构建用户界面,与传统的使用WebView相比,不 ...

  10. PHP 的异步并行和协程 C 扩展 Swoole (附链接)

    PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列, ...