Blogger Information
Blog 43
fans 1
comment 0
visits 33802
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
利用Laravel进行数据库原生的增删改查操作
蔚蓝世纪
Original
1001 people have browsed it

一、使用composer安装laravel

  1. create-project laravel/laravel mirrorapp

安装后的文件夹效果:

二、Laravel数据库原生的增删改查
Laravel中的MVC框架:

  1. Mlarevel没有模型目录,默认把 Eloquent 的模型放在 app 目录下,并允许开发人员将它们放在其他地方。
  2. V: larevel/resources/test.blade.php
  3. Clarevel/App/Http/Controllers

在larevel/App/Http/Controllers创建Home.php文件

  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. //打印二维数组
  11. $res = DB::select('select * from article');
  12. $lists = [];
  13. foreach($res as $val){
  14. $lists[] = (Array)$val;
  15. }
  16. $data['result'] = $lists;
  17. echo '<pre>';
  18. print_r($data);
  19. return view('test',$data);;
  20. }
  21. //数据库查询[原生查询]
  22. public function get(){
  23. //select * from article
  24. echo '<pre>';
  25. $res = DB::select('select * from article where id>2');
  26. print_r($res);
  27. // return view('test',$data);
  28. }
  29. //数据库更新操作[原生更新]
  30. public function test_updateadafd(){
  31. $res = DB::update('update article set title="我知道,最好的语言:php,25岁了!" where id=2');
  32. var_dump($res);
  33. }
  34. //新增数据[原生新增]
  35. public function insert_mytest(){
  36. $res = DB::insert('insert article(`id`,`title`)values(5,"excel鼠标滚轮上下失灵")');
  37. var_dump($res);
  38. }
  39. //删除数据【原生删除】
  40. public function delete_asdf(){
  41. $res = DB::delete('delete from article where id=2');
  42. var_dump($res);
  43. }
  44. }

在larevel/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('welcome'); //视频 //视频引擎
  15. return view('test');
  16. });
  17. Route::get('/Home/index','Home@index');
  18. Route::get('/admins/article/lists','admins\Article@lists');
  19. Route::get('/dbselect','Home@get');
  20. Route::get('/dbupdate','Home@test_updateadafd');
  21. Route::get('/dbinsert','Home@insert_mytest');
  22. Route::get('/dbdelete','Home@delete_asdf');

输出效果:

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!