博客列表 >laravel数据库原生查询、更新、新增、删除

laravel数据库原生查询、更新、新增、删除

王珂
王珂原创
2020年07月12日 15:52:071452浏览

laravel数据库原生查询、更新、新增、删除

home.php

  1. <?php
  2. // 命名空间:和目录对应
  3. // 类名称和文件名称一致
  4. namespace App\Http\Controllers;
  5. use Illuminate\Support\Facades\DB;
  6. class Home extends Controller {
  7. public function index(){
  8. $time = date('Y-m-d H:i:s');
  9. $data = [];
  10. $data['time'] = $time;
  11. $data['name'] = 'laravel中文';
  12. //第一种方法,数组当做参数
  13. //return view('test',$data);
  14. //第二种方法,with
  15. return view('test')->with('time',$time)->with('name','with链式调用');
  16. }
  17. //数据库查询(原生查询)
  18. //::静态方法,->需要new
  19. //DB::select查询的结果第一层是数组,第二层是对象,get()将结果转为两维数组,getall()未转。
  20. public function get(){
  21. //select * from article
  22. echo '<pre>';
  23. $res = DB::select('select * from article');
  24. //print_r($res);
  25. $lists = [];
  26. foreach($res as $val){
  27. $lists[] = (array)$val;
  28. }
  29. $data['result']=$lists;
  30. //print_r($data);
  31. return view('dbselect',$data);
  32. }
  33. public function getall(){
  34. //select * from article
  35. echo '<pre>';
  36. $res = DB::select('select * from article');
  37. //print_r($res);
  38. $data['result']=$res;
  39. print_r($data);
  40. return view('dbselect1',$data);
  41. }
  42. //数据库更新(原生更新)
  43. public function test_updateadafd(){
  44. //select * from article
  45. echo '<pre>';
  46. $res = DB::update('update article set title="999" where id =2');
  47. var_dump($res);
  48. }
  49. //新增(原生)
  50. public function insert_mytest(){
  51. $res = DB::insert('insert article(`id`,`title`) values(5,"5款简洁的layui后台管理模板推荐")');
  52. var_dump($res);
  53. }
  54. //删除(原生)
  55. public function delete_asdf(){
  56. $res = DB::delete('delete from article where id=5');
  57. var_dump($res);
  58. }
  59. //高级数据库查询方法
  60. public function finds(){
  61. $res = DB::table('article')->where('id',3)->first();
  62. echo '<pre>';
  63. print_r($res);
  64. }
  65. }

web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/', function () {
  4. //echo date('Y-m-d H:i:s');
  5. //return view('welcome');
  6. return view('test');
  7. });
  8. Route::get('/article/p/aaa', 'Home@index');
  9. Route::get('/admin/article/lists', 'Admin\Article@lists');
  10. Route::get('/dbselect', 'Home@get');
  11. Route::get('/dbselect1', 'Home@getall');
  12. Route::get('/dbupdate', 'Home@test_updateadafd');
  13. Route::get('/dbinsert', 'Home@insert_mytest');
  14. Route::get('/dbdelete', 'Home@delete_asdf');
  15. Route::get('/dbfinds', 'Home@finds');

test.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>my test</title>
  5. </head>
  6. <body>
  7. <h3><?php echo $name;?></h3>
  8. php中文网
  9. <div><?php echo $time;?></div>
  10. </body>
  11. </html>

dbselect.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>my test</title>
  5. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  6. </head>
  7. <body>
  8. <table class="layui-table">
  9. <thead>
  10. <tr>
  11. <th>ID</td>
  12. <th>标题</td>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <?php foreach($result as $val){?>
  17. <tr>
  18. <th><?php echo $val['id']?></td>
  19. <th><?php echo $val['title']?></td>
  20. </tr>
  21. <?php }?>
  22. </tbody>
  23. </table>
  24. </body>
  25. </html>

dbselect1.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>my test</title>
  5. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  6. </head>
  7. <body>
  8. <table class="layui-table">
  9. <thead>
  10. <tr>
  11. <th>ID</td>
  12. <th>标题</td>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <?php foreach($result as $val){?>
  17. <tr>
  18. <th><?php echo $val->id?></td>
  19. <th><?php echo $val->title?></td>
  20. </tr>
  21. <?php }?>
  22. </tbody>
  23. </table>
  24. </body>
  25. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议