博客列表 >laravel基础(数据库、增、删、改、查、原生查询)

laravel基础(数据库、增、删、改、查、原生查询)

w™下載一個妳
w™下載一個妳原创
2020年06月23日 19:57:55981浏览

laravel基础

1.laravel数据库原生,增、删、改、查、和高级查询

1.1 laravel路由代码:

  1. <?php
  2. use App\Http\Controllers\Home;
  3. use Illuminate\Support\Facades\Route;
  4. /*
  5. |--------------------------------------------------------------------------
  6. | Web Routes
  7. |--------------------------------------------------------------------------
  8. |
  9. | Here is where you can register web routes for your application. These
  10. | routes are loaded by the RouteServiceProvider within a group which
  11. | contains the "web" middleware group. Now create something great!
  12. |
  13. */
  14. Route::get('/', function () {
  15. //echo date('Y-m-d H:i:s');
  16. return view('welcome');//视图,视图引擎blade
  17. //return view('test');
  18. });
  19. /*Route::get('/article/p/aaa',function(){
  20. return 'php.cn';
  21. });*/
  22. Route::get('/home/index','Home@index');
  23. Route::get('/admins/article/lists','admins\Article@lists');
  24. Route::get('/dbselect','Home@get');
  25. Route::get('/dbupdate','Home@test_updateadafd');
  26. Route::get('/dbinsert','Home@insert_mytest');
  27. Route::get('/dbdelete','Home@delete_asdf');
  28. Route::get('/dbfinds','Home@finds');

1.2 laravel控制器代码:

  1. <?php
  2. //1.命名空间:和目录对应
  3. //2.类名称和文件文件名称一致
  4. namespace App\Http\Controllers;
  5. use Illuminate\Support\Facades\DB;
  6. class Home extends Controller{
  7. public function index(){
  8. //return 'www.php.cn';
  9. $time = date('Y-m-d H:i:s');
  10. $res = DB::select('select * from article');
  11. $lists = [];
  12. foreach($res as $val){
  13. $lists[] = (array)$val;
  14. }
  15. $data['result'] = $lists;
  16. // echo '<pre>';
  17. // print_r($data);
  18. return view('test',$data);
  19. // return view('test')->with('time',$time)->with('name','laravel应用程序');
  20. }
  21. //数据库查询[原生查询]
  22. public function get(){
  23. //select * from article
  24. $res = DB::select('select * from article where id>3');
  25. echo '<pre>';
  26. print_r($res);
  27. }
  28. //数据库更新操作【原生更新】
  29. public function test_updateadafd(){
  30. $res = DB::update('update article set title="最污技术,我竟然秒懂" where id=3');
  31. var_dump($res);
  32. }
  33. //新增数据[原生新增]
  34. public function insert_mytest(){
  35. $res = DB::insert('insert article(`id`,`title`)values(5,"excel鼠标上下失灵")');
  36. var_dump($res);
  37. }
  38. //删除数据【原生删除】
  39. public function delete_asdf(){
  40. $res = DB::delete('delete from article where id=2');
  41. var_dump($res);
  42. }
  43. //高级数据库查询方法【链式调用】
  44. public function finds(){
  45. $res = DB::table('article')->where('id',3)->first();
  46. echo '<pre>';
  47. print_r($res);
  48. }
  49. }

1.2 laravel视图代码:

  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>Document</title>
  8. </head>
  9. <body>
  10. <table class="layui-table">
  11. <thead>
  12. <tr>
  13. <td>ID</td>
  14. <td>标题</td>
  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>
  28. <?php
  29. namespace App\Http\Controllers\admins;
  30. use App\Http\Controllers\Controller;
  31. use Illuminate\Http\Request;
  32. class Article extends Controller
  33. {
  34. public function lists(){
  35. echo '后台文章';
  36. }
  37. }

2.数据库 增、删、改、查演练图片:



3.数据库创建、配置图:


3.学习总结:

通过这节课的学习,深刻了解到了laravel框架的强大,以及使用的实效性,跟着老师用laravel做了一遍,增,删,改,查,感觉效果很好,还学习了怎么利用laravel链接数据库,在使用数据库前将数据库配置、创建、以及对应的表先做好,再进行相关操作,自己还需要多加练习。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议