Blogger Information
Blog 52
fans 0
comment 3
visits 42422
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel框架的数据增查改删mvc
王小飞
Original
873 people have browsed it

首先设置数据库

  • 找到数据库文件 .env
  • 第9行到14行

    1. DB_CONNECTION=mysql
    2. DB_HOST=127.0.0.1
    3. DB_PORT=3306
    4. DB_DATABASE=php
    5. DB_USERNAME=root
    6. DB_PASSWORD=123456
  • 修改成自己的数据库账户

创建控制器 Home.php

  • 在app/Http/Controllers 目录下创建控制器
  • 可以用命令行的方式创建 也可以新建文件的方式创建

控制器代码:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use DB;
  4. class Home extends Controller{
  5. // 数据库查询
  6. public function get(){
  7. echo '<pre>';
  8. $res = DB::select('select * from users');
  9. print_r($res);
  10. }
  11. //数据库更新
  12. public function gxupdata(){
  13. $res = DB::update('update users set name="我知道,最好的语言:php25岁了" where id=4');
  14. var_dump($res);
  15. }
  16. //数据库新增
  17. public function xzupdata(){
  18. $res = DB::insert('insert users(`name`,`email`,`password`)values("mignzi","ad@s.com","aginogsg")');
  19. var_dump($res);
  20. }
  21. //删除数据
  22. public function delete(){
  23. $res = DB::delete("delete from users where id=2");
  24. var_dump($res);
  25. }
  26. public function index(){
  27. // 加载v视图文件
  28. $res = DB::select('SELECT * FROM `jizhang`');
  29. $lists = [];
  30. foreach ($res as $val){
  31. $lists[] = (array)$val;
  32. }
  33. $data['result'] = $lists;
  34. // print_r($val);
  35. return view('v', $data);
  36. }
  37. }

设置路由

  • 路径public/routes/web.php

路由代码:

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Web Routes
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here is where you can register web routes for your application. These
  9. | routes are loaded by the RouteServiceProvider within a group which
  10. | contains the "web" middleware group. Now create something great!
  11. |
  12. */
  13. Route::get('/', function () {
  14. return view('v');
  15. // echo date('Y-m-d h:i:s');
  16. });
  17. Route::get('/article/p/aaa', function () {
  18. // return view('v');
  19. echo date('Y-m-d h:i:s');
  20. });
  21. Route::get('home/index','Home@index');
  22. Route::get('/db','Home@get');
  23. Route::get('/dbgx','Home@gxupdata');
  24. Route::get('/dbxz','Home@zxupdata');
  25. Route::get('/dbsc','Home@delete');

添加视图文件

  • 路径public/resources/views/v.blade.php

文件代码:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>数据库操作</title>
  6. <link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
  7. <script src="layui/layui.js"></script>
  8. </head>
  9. <body>
  10. <table class="layui-table">
  11. <thead>
  12. <tr>
  13. <td>ID</td>
  14. <td>金额</td>
  15. <td>账户</td>
  16. <td>成员</td>
  17. <td>备注</td>
  18. <td>成员id</td>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <?php foreach ($result as $val): ?>
  23. <tr>
  24. <td><?php echo $val['id']; ?></td>
  25. <td><?php echo $val['jine']; ?></td>
  26. <td><?php echo $val['zhanghu']; ?></td>
  27. <td><?php echo $val['chengyuan']; ?></td>
  28. <td><?php echo $val['beizhu']; ?></td>
  29. <td><?php echo $val['yonghuid']; ?></td>
  30. </tr>
  31. <?php endforeach; ?>
  32. </tbody>
  33. </table>
  34. </body>
  35. </html>

效果:

总结:本节课学习了 laravel框架 的mvc 与数据库查询删除修改的操作,更详细了学习了laravel框架。

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