<?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. Unrecognized header format %

    <VirtualHost *:*> RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME} </ ...

  2. Java总结转载,持续更新。。。

    1.Java中内存划分 https://www.cnblogs.com/yanglongbo/p/10981680.html

  3. LG2679 「NOIP2015」子串 线性DP

    问题描述 LG2679 题解 设\(opt[i][j]\)代表A串前\(i\)个,匹配\(B\)串前\(j\)个,选择了\(k\)个子串的方案数. 转移用前缀和优化一下. \(\mathrm{Code ...

  4. 【STM32H7教程】第30章 STM32H7的USART应用之八个串口FIFO实现

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第30章       STM32H7的USART应用之八个串口 ...

  5. pyqt添加启动等待界面

    一.实验环境 1.Windows7x64_SP1 2.anaconda3.7 + python3.7(anaconda集成,不需单独安装) 3.pyinstaller3.5 #使用pyinstalle ...

  6. tensorflow2.0安装

    版本: python3.5 Anaconda 4.2.0 tensorflow2.0 cpu版本 1.安装命令 pip3 install tensorflow==2.0.0.0a0 -i https: ...

  7. Linux常用命令之文件编辑命令vim

    vi命令 vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多 ...

  8. ASP.NET Core Identity 的示例

    1. appsettings.json { "ConnectionStrings": { "DefaultConnection": "Server=( ...

  9. Newtonsoft.Json 序列化踩坑之 IEnumerable

    Newtonsoft.Json 序列化踩坑之 IEnumerable Intro Newtonsoft.Json 是 .NET 下最受欢迎 JSON 操作库,使用起来也是非常方便,有时候也可能会不小心 ...

  10. C#委托内部使用局部的变量的问题

    一. 引子 先来看如下代码: ; Action action1 = () => { Console.WriteLine("打印一下i的值:" + i); }; i = ; A ...