laravel5.6操作数据curd写法(查询构建器)

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

  1 <?php
  2 //laravel5.6  语法 demo示例
  3 
  4 namespace App\Http\Controllers;//命名该控制App空间下名称
  5 
  6 use Illuminate\Support\Facades\DB;//使用DB操作数据库
  7 use App\Http\Controllers\Controller;//继承基础控制器
  8 
  9 class UserController extends Controller
 10 {
 11     /**
 12      * 展示应用的用户列表.
 13      *
 14      * @return Response
 15      */
 16     public function index()
 17     {
 18         //DB使用为每种操作提供了相应方法:select(查),update(修改),insert(插入),delete(删除),statement(声明)
 19         //建议占位符,其他框架通用性强
 20         //原生sql写法
 21         $data = DB::select('select * from users where id = :id and name = :name ',[':id' => 1,':name'  =>'测试']);
 22 
 23         //查方法
 24         //get()  方法获取表中所有记录(获取多行多列)
 25         $data = DB::table('users')->get();
 26 
 27         //first()  方法将会返回单个对象(获取一行一列)
 28         //where()  方法查询指定条件对象
 29         $data = DB::table('users')->where('id','name','3','测试')->first();
 30 
 31         //select() 方法可以查询指定自定义字段
 32         $data = DB::table('users')->select('id','name', 'email')->get();
 33 
 34         //value() 方法从结果中获取单个值,该方法会直接返回指定列的值:
 35         $data = DB::table('users')->where('name','测试')->value('email');
 36 
 37         //pluck()  方法获取单个列值的数组
 38         $data = DB::table('users')->pluck('name');
 39 
 40         //count() 统计数量
 41         $data = DB::table('users')->count();
 42 
 43         //exists()  方法来判断匹配查询条件的结果是否存在
 44         $data=DB::table('users')->where('id', 1)->exists();
 45 
 46         //join() 方法连表查询
 47         $data = DB::table('users')
 48             ->join('ceshi', 'users.id', '=', 'ceshi.id')
 49             ->select('users.*', 'ceshi.name')
 50             ->get();
 51 
 52         //leftJoin() 方法左连表查询
 53         $data = DB::table('users')
 54             ->leftJoin('ceshi', 'users.id', '=', 'ceshi.id')
 55             ->select('users.*', 'ceshi.name')
 56             ->get();
 57 
 58         //where() 参数说明:(一)参数是列名,(二)参数是操作符,(三)参数是该列要比较的值
 59         $data = DB::table('users')
 60             ->where('id', '>=', 1)
 61             ->where('name', 'like', '测试%')
 62             ->get();
 63 
 64         //传递条件数组到where中写法,建议多where查询使用这个方法
 65         $data = DB::table('users')
 66             ->where([
 67                 ['id', '>=', 1],
 68                 ['name', 'like', '测试%']
 69             ])
 70             ->get();
 71 
 72         //whereBetween() 方法验证列值是否在给定值之间
 73         $data = DB::table('users')
 74             ->whereBetween('id', [1, 3])->get();
 75 
 76         //whereIn 方法验证给定列的值是否在给定数组中:
 77         $data = DB::table('users')
 78             ->whereIn('id', [1, 2, 3])
 79             ->get();
 80 
 81         //orderBy() 方法排序
 82         $data = DB::table('users')
 83             ->orderBy('id', 'desc')
 84             ->get();
 85 
 86         //insert()      方法插入记录到数据表
 87         //insertGetId() 方法插入记录并返回自增ID值
 88         $data=DB::table('users')->insert(
 89             [
 90                 'name'=>'测试',
 91                 'email' => 'ceshi.com',
 92                 'password' => 'ceshi'
 93             ]
 94         );
 95 
 96         //update() 方法修改记录
 97         $data =DB::table('users')
 98             ->where('id', 1)
 99             ->update(['name' => '测试']);
100 
101         //delete() 方法删除记录
102         $data=DB::table('users')->where('id', '>', 10)->delete();
103 
104 
105         //paginate() 方法分页 每页显示数量
106         //注意:目前使用 groupBy 的分页操作不能被Laravel有效执行
107         $data = DB::table('users')->paginate(2);
108 
109 
110         //前台分页中链接附加参数实现分页
111         $getName = $GET['name']?:'';
112         $data = DB::table('users')
113              ->select('id','name','age')
114              ->where('name', 'like', $getName.'%')
115              ->paginate(2);
116 
117         //返回给前端视图数据
118         return $this->view('index',['data'=>$data,'namePage'=>$getName]);
119 
120         //前端引用代码  
121         //appends 方法添加查询参数到分页链接查询字符串; 添加 &name=$namePage到每个分页链接中.
122         {{ $data->appends(['name' => $namePage])->links() }}
123 
124 
125         //simplePaginate() 方法分页视图中简单的显示“下一页”和“上一页”链接
126         $data = DB::table('users')->simplePaginate(2);
127         //返回给前端视图数据
128         return $this->view('index',['data'=>$data]);
129         //前端简单引用代码 
130         <div class="container">
131         @foreach ($users as $user)
132             {{ $user->name }}
133         @endforeach
134         </div>
135         {{ $data->links() }}
136 
137 
138         //原生分页写法
139         $page = 2;
140         $pageSize = 1;
141         $offset = ($page - 1) * $pageSize;
142         $result = DB::table('picasa')
143             ->where('title', 'like', '%'.$title.'%')
144             ->offset($offset)
145             ->limit($pageSize)
146             ->get();
147 
148         //返回数据视图文件
149         return $this->view('index', ['result' => $result]);
150 
151     }
152 }

 

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

1 当select和groupBy中列表不一致时候会报错。mysql从5.7以后,默认开启group by的严格模式。
2 
3 解决方法:找到config/database​.php 在mysql下面把'strict' => true,改为false。[建议不要修改。写对正确操作语法。]
4 
5 例如:
6 $booked = DB::table('booked_user')
7     ->select('game_id', DB::raw('count(*) as total'))
8     ->groupBy('game_id')
9     ->get();

 

开启sql查询日志

1  DB::connection()->enableQueryLog();//开启QueryLog
2  $data = DB::table('users')->select('id','name', 'email')->get();//执行sql
3  dump(DB::getQueryLog());//sql语句和查询时间

 

写入日志信息

八种日志级别:emergency、alert、critical、error、warning、 notice、info 和 debug
默认日志存放位置: /storage/logs/laravel.log
引用: use Illuminate\Support\Facades\Log;

1 Log::emergency(string $message, array $context = []);
2 Log::alert(string $message, array $context = []);
3 Log::critical(string $message, array $context = []);
4 Log::error(string $message, array $context = []);
5 Log::warning(string $message, array $context = []);
6 Log::notice(string $message, array $context = []);
7 Log::info(string $message, array $context = []);
8 Log::debug(string $message, array $context = []);

 

 laravel5.6 操作数据ORM链接:  请点击跳转

 

posted @ 2018-07-13 16:38  echo曦  阅读(1517)  评论(0编辑  收藏  举报