Blogger Information
Blog 28
fans 0
comment 0
visits 25985
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel MVC流程和laravel中数据库原生操作
,多思曩惜,
Original
843 people have browsed it
  • 当前Laravel 支持四种数据库:
    • MySQL 5.6+
    • PostgreSQL 9.4+
    • SQLite 3.8.8+
    • SQL Server 2017+

Laravel中数据库配置

  • 数据库所在的文件在.env
  1. DB_CONNECTION=mysql //数据库类型
  2. DB_HOST=127.0.0.1 //地址
  3. DB_PORT=3306 //端口号
  4. DB_DATABASE=laravel //数据库名
  5. DB_USERNAME=root //数据库用户名
  6. DB_PASSWORD=root //数据库密码

在Laravel中使用原生的数据库操作

1.数据库查询

  • web 路由代码演示
  1. Route::get('/', function () {
  2. // return view('welcome');
  3. return view('test');
  4. // echo date("Y-M-D");
  5. });
  6. // Route::get('/active/p/aaa',function(){
  7. // return 'ppp';
  8. // });
  9. Route::get('/home/index','Home@index');
  10. Route::get('/admins/article/lists','admin\Article@lists');
  11. // 查询数据库
  12. Route::get('/dbselect','Home@get');
  13. // 数据库更新
  14. Route::get('/update','Home@test_update');
  15. // 数据新增
  16. Route::get('/inset','Home@test_inset');
  17. // 数据删除
  18. Route::get('/delete','Home@test_delete');
  19. Route::get('/find','Home@find');
  • 视图test.blade.php代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  7. <title>test</title>
  8. </head>
  9. <body>
  10. <table class="layui-table">
  11. <thead>
  12. <tr>
  13. <th>id</th>
  14. <TH>标题</TH>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <?php foreach($result as $val){?>
  19. <tr>
  20. <td><?php echo $val['id'] ?></td>
  21. <td><?php echo $val['title']?></td>
  22. </tr>
  23. <?php } ?>
  24. </tbody>
  25. </table>
  26. </body>
  27. </html>
  • Home.php文件下的演示

数据库查询

  1. // 数据库查询原生查询
  2. public function get(){
  3. // select * from atitle
  4. echo '<pre>';
  5. $res = DB::select('select * from atitle where id>2');
  6. //$res = \DB::select('select * from atitle);
  7. print_r($res);
  8. }

数据库更新

  1. // 数据库更新操作原生
  2. public function test_update(){
  3. $res = DB::update('update atitle set title="我知道" where id=2');
  4. var_dump($res);
  5. }

数据库新增

  1. // 数据库新增原生
  2. public function test_inset(){
  3. $res = DB::insert('insert atitle(`id`,`title`)values(5,"sssss")');
  4. var_dump($res);
  5. }

数据库删除

  1. // 数据库删除原生
  2. public function test_delete(){
  3. $res = DB::delete('delete from atitle where id=1');
  4. var_dump($res);
  5. }

数据库的链式调用(laravel)

  1. // 高级数据库查询方法(链式调用)
  2. public function find(){
  3. // select * from atitle where id=3
  4. $res=DB::table('atitle')->where('id',3)->first();
  5. echo $res->title;
  6. echo '<pre>';
  7. print_r($res);
  8. }

总结

  • 开始使用时,并不知道如何调用路由和使用视图等操作。
  • 看多视频几,并实际操作之后才了解,并学会使用。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:不容易,还在坚持写
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post