程序运行截图如下:
对应的表结构是这样的:
/*
SQLyog Ultimate v12.09 (64 bit)
MySQL - 5.7.12 : Database - laravel
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`laravel` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `laravel`;
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*Data for the table `student` */
insert into `student`(`id`,`name`,`age`) values (2,'小张',19),(3,'小明',18),(4,'小张',19);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
源码如下:
路由web.php添加:
Route::prefix('student')->group(function(){
......
......
Route::get('qbInsert', 'StudentController@qbInsert');
Route::get('qbSelect', 'StudentController@qbSelect');
Route::get('qbModify', 'StudentController@qbModify');
Route::get('qbDelete', 'StudentController@qbDelete');
});
这个config/database.php是数据库相关的设置,目前不用改,对应mysql是这样的。
需要修改的是
.env
修改为如下:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
新建StudentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller{
......
......
......
......
public function qbSelect(){
//get是获取所有数据
$students = DB::table('student')->get();
dd($students);
//first查询第一条数据
$student = DB::table('student')->first();
dd($student);
//first + 排序
$student = DB::table('student')
->orderBy('id', 'desc')
->first();
dd($student);
//加where
$students = DB::table('student')
->whereRaw('id >= ? and age > ?', [5, 16])
->get();
dd($student);
//pluck返回结果集中指定字段
$names = DB::table('student')->pluck('name');
dd($names);
//lists一样的效果,但可以指定某个键,作为下标
$names = DB::table('student')->lists('name', 'id');
dd($names);
//select指定查询的字段
$students = DB::table('student')->select('id', 'name', 'age')->get();
dd($students);
//chunk:每次查指定数据,比如2条,防止一次性查出,内存爆了,在某个地方停止,就return false
echo '<pre>';
DB::table('student')->chunk(2, function($students){
var_dump($students);
//条件,停止就返回false
//return false;
});
//聚合函数
var_dump(DB::table('student')->count());
var_dump(DB::table('student')->max('age'));
var_dump(DB::table('student')->min('age'));
var_dump(DB::table('student')->avg('age'));
var_dump(DB::table('student')->sum('age'));
return "qbSelect";
}
public function qbDelete(){
$num = DB::table('student')
->where('id', 2)
->delete();
var_dump($num);
$num = DB::table('student')
->where('id', '>=', 4)
->delete();
var_dump($num);
//删除所有
DB::table('student')->truncate();
return "qbDelete";
}
public function qbModify(){
$num = DB::table('student')
->where('id', 2)
->update(['age' => 50]);
DB::table('student')->increment('age', 2);
DB::table('student')->decrement('age', 3);
var_dump($num);
//同时修改
$num = DB::table('student')
->where('id', 12)
->decrement('age', 2, ['name' => '呵呵']);
var_dump($num);
return "qbModify";
}
public function qbInsert(){
$bool = DB::table('student')->insert(
['name' => '小白', 'age' => 16]
);
var_dump($bool);
$id = DB::table('student')->insertGetId(
['name' => '小黄', 'age' => 15]
);
var_dump($id);
$bool = DB::table('student')->insert([
['name' => 'name1', 'age' => 30],
['name' => 'name2', 'age' => 31]
]);
var_dump($bool);
return "qbInsert";
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://it1995.blog.csdn.net/article/details/124659113
内容来源于网络,如有侵权,请联系作者删除!