laravel5.6框架操作数据curd写法(查询构建器)实例分析

2025-05-29 0 32

本文实例讲述了laravel5.6框架操作数据curd写法(查询构建器)。分享给大家供大家参考,具体如下:

laravel5.6 数据库操作-查询构建器

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120
<?php

//laravel5.6 语法 demo示例

namespace AppHttpControllers;//命名该控制App空间下名称

use IlluminateSupportFacadesDB;//使用DB操作数据库

use AppHttpControllersController;//继承基础控制器

class UserController extends Controller

{

/**

* 展示应用的用户列表.

*

* @return Response

*/

public function index()

{

//DB使用为每种操作提供了相应方法:select(查),update(修改),insert(插入),delete(删除),statement(声明)

//建议占位符,其他框架通用性强

//原生sql写法

$data = DB::select('select * from users where id = :id and name = :name ',[':id' => 1,':name' =>'测试']);

//查方法

//get() 方法获取表中所有记录(获取多行多列)

$data = DB::table('users')->get();

//first() 方法将会返回单个对象(获取一行一列)

//where() 方法查询指定条件对象

$data = DB::table('users')->where('id','name','3','测试')->first();

//select() 方法可以查询指定自定义字段

$data = DB::table('users')->select('id','name', 'email')->get();

//value() 方法从结果中获取单个值,该方法会直接返回指定列的值:

$data = DB::table('users')->where('name','测试')->value('email');

//pluck() 方法获取单个列值的数组

$data = DB::table('users')->pluck('name');

//count() 统计数量

$data = DB::table('users')->count();

//exists() 方法来判断匹配查询条件的结果是否存在

$data=DB::table('users')->where('id', 1)->exists();

//join() 方法连表查询

$data = DB::table('users')

->join('ceshi', 'users.id', '=', 'ceshi.id')

->select('users.*', 'ceshi.name')

->get();

//leftJoin() 方法左连表查询

$data = DB::table('users')

->leftJoin('ceshi', 'users.id', '=', 'ceshi.id')

->select('users.*', 'ceshi.name')

->get();

//where() 参数说明:(一)参数是列名,(二)参数是操作符,(三)参数是该列要比较的值

$data = DB::table('users')

->where('id', '>=', 1)

->where('name', 'like', '测试%')

->get();

//传递条件数组到where中写法,建议多where查询使用这个方法

$data = DB::table('users')

->where([

['id', '>=', 1],

['name', 'like', '测试%']

])

->get();

//whereBetween() 方法验证列值是否在给定值之间

$data = DB::table('users')

->whereBetween('id', [1, 3])->get();

//whereIn 方法验证给定列的值是否在给定数组中:

$data = DB::table('users')

->whereIn('id', [1, 2, 3])

->get();

//orderBy() 方法排序

$data = DB::table('users')

->orderBy('id', 'desc')

->get();

//insert() 方法插入记录到数据表

//insertGetId() 方法插入记录并返回自增ID值

$data=DB::table('users')->insert(

[

'name'=>'测试',

'email' => 'ceshi.com',

'password' => 'ceshi'

]

);

//update() 方法修改记录

$data =DB::table('users')

->where('id', 1)

->update(['name' => '测试']);

//delete() 方法删除记录

$data=DB::table('users')->where('id', '>', 10)->delete();

//paginate() 方法分页 每页显示数量

//注意:目前使用 groupBy 的分页操作不能被Laravel有效执行

$data = DB::table('users')->paginate(2);

//前台分页中链接附加参数实现分页

$getName = $GET['name']?:'';

$data = DB::table('users')

->select('id','name','age')

->where('name', 'like', $getName.'%')

->paginate(2);

//返回给前端视图数据

return $this->view('index',['data'=>$data,'namePage'=>$getName]);

//前端引用代码

//appends 方法添加查询参数到分页链接查询字符串; 添加 &name=$namePage到每个分页链接中.

{{ $data->appends(['name' => $namePage])->links() }}

//simplePaginate() 方法分页视图中简单的显示“下一页”和“上一页”链接

$data = DB::table('users')->simplePaginate(2);

//返回给前端视图数据

return $this->view('index',['data'=>$data]);

//前端简单引用代码

<div class="container">

@foreach ($users as $user)

{{ $user->name }}

@endforeach

</div>

{{ $data->links() }}

//原生分页写法

$page = 2;

$pageSize = 1;

$offset = ($page - 1) * $pageSize;

$result = DB::table('picasa')

->where('title', 'like', '%'.$title.'%')

->offset($offset)

->limit($pageSize)

->get();

//返回数据视图文件

return $this->view('index', ['result' => $result]);

}

}

groupBy 对查询结果进行分组出现问题

当select和groupBy中列表不一致时候会报错。mysql从5.7以后,默认开启group by的严格模式。

解决方法:找到config/database​.php 在mysql下面把'strict' => true,改为false。[建议不要修改。写对正确操作语法。]

例如:

?

1

2

3

4
$booked = DB::table('booked_user')

->select('game_id', DB::raw('count(*) as total'))

->groupBy('game_id')

->get();

开启sql查询日志

?

1

2

3
DB::connection()->enableQueryLog();//开启QueryLog

$data = DB::table('users')->select('id','name', 'email')->get();//执行sql

dump(DB::getQueryLog());//sql语句和查询时间

写入日志信息

八种日志级别:emergency、alert、critical、error、warning、 notice、info 和 debug
默认日志存放位置: /storage/logs/laravel.log

引用: use IlluminateSupportFacadesLog;

?

1

2

3

4

5

6

7

8
Log::emergency(string $message, array $context = []);

Log::alert(string $message, array $context = []);

Log::critical(string $message, array $context = []);

Log::error(string $message, array $context = []);

Log::warning(string $message, array $context = []);

Log::notice(string $message, array $context = []);

Log::info(string $message, array $context = []);

Log::debug(string $message, array $context = []);

laravel5.6 操作数据ORM

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

原文链接:https://www.cnblogs.com/cxx8181602/p/9305830.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 laravel5.6框架操作数据curd写法(查询构建器)实例分析 https://www.kuaiidc.com/104692.html

相关文章

发表评论
暂无评论