Blogger Information
Blog 33
fans 0
comment 0
visits 19830
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel数据库访问--php培训九期线上班
取个名字真难
Original
621 people have browsed it

、laravel\app\Http\Controllers\Home.php文件

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\View\View;
  6. class Home extends Controller
  7. {
  8. // 查询
  9. function mySele()
  10. {
  11. $data=DB::select('select * from article ');
  12. return view('index',['data'=>$data]);
  13. }
  14. // 用查询构造器
  15. function mySele2(){
  16. $data=DB::table('article')->where('id','>','5')->get();
  17. return view('index',['data'=>$data]);
  18. }
  19. //插入
  20. function myErt(){
  21. $data2=DB::insert ('insert into article (id,title) values(11,"新插入进来的") ');
  22. return view('index',['data2'=>$data2]);
  23. }
  24. //用插入构造器
  25. //插入一条记录
  26. function myErt2()
  27. {
  28. $data2 = DB::table('article')->insert(['id' => 9, 'title' => '新来的']);
  29. return view('index',['data2'=>$data2]);
  30. }
  31. //插入多条记录
  32. function myErt3(){
  33. $data2 = DB::table('article')->insert(
  34. [ [ 'title' => '新来的'], ['title'=>'插入的第10条记录'], ['title'=>'插入的第11条记录'] ]
  35. );
  36. return view('index',['data2'=>$data2]);
  37. }
  38. //insertGetId只能插入一条记录
  39. function myErt4(){
  40. $data2 = DB::table('article')->insertGetId(['id' => 12, 'title' => '新来的']);
  41. return view('index',['data2'=>$data2]);
  42. }
  43. //insertGetId插入多条记录
  44. function myErt5(){
  45. $data5 = DB::table('article')->insert([
  46. [ 'title' => '新来的2'],
  47. [ 'title' => '新来的3'],
  48. ['title'=>'插入的第15条记录'],
  49. ]);
  50. return view('index',['data5'=>$data5]);
  51. }
  52. //更新
  53. function myUp(){
  54. $data3=DB::update('update article set title ="我是行进过update过的" where id > 8');
  55. return view('index',['data3'=>$data3]);
  56. }
  57. ///用构造器更新
  58. function myUp2(){
  59. $data3=DB::table('article')->where('id',9)->update(['title'=>'uadate更新来的']);
  60. return view('index',['data3'=>$data3]);
  61. }
  62. // 删除
  63. function myDe(){
  64. $data4=DB::delete('delete from article where id>8 ');
  65. return view('index',['data4'=>$data4]);
  66. }
  67. ///用构造器删除
  68. function myDe2(){
  69. $data4=DB::table('article')->where('id','>','8')->delete();
  70. return view('index',['data4'=>$data4]);
  71. }
  72. //whereIn
  73. function myWhere(){
  74. $data5 = DB::table('article')->whereIn('id', [1, 2, 3])->get()->toArray();
  75. return view('index',['data5'=>$data5]);
  76. }
  77. }

\laravel\routes\web.php文件

  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Web Routes
  5. |--------------------------------------------------------------------------
  6. |
  7. | Here is where you can register web routes for your application. These
  8. | routes are loaded by the RouteServiceProvider within a group which
  9. | contains the "web" middleware group. Now create something great!
  10. |
  11. */
  12. Route::get('/', function () {
  13. return view('welcome');
  14. });
  15. Route::get('/show','Home@mySele');
  16. Route::get('/show2','Home@mySele2');
  17. Route::get('/myinsert','Home@myErt');
  18. Route::get('/myinsert2','Home@myErt2');
  19. Route::get('/myinsert3','Home@myErt3');
  20. Route::get('/myinsert4','Home@myErt4');
  21. Route::get('/myinsert5','Home@myErt5');
  22. Route::get('/myUp','Home@myUp');
  23. Route::get('/myUp2','Home@myUp2');
  24. Route::get('/myDe','Home@myDe');
  25. Route::get('/myDe2','Home@myDe2');
  26. Route::get('/myWhere','Home@myWhere');

\laravel\resources\views\index.blade.php文件

  1. <div>
  2. <p>输出如果id>3就输出title否者就输出id</p>
  3. @foreach($data as $id)
  4. @if($id->id>3)
  5. <p>{{$id->id}}</p>
  6. @else
  7. <p>{{$id->title}}</p>
  8. @endif
  9. @endforeach
  10. {{var_dump($data5)}}
  11. {{var_dump($data4)}}
  12. {{var_dump($data3)}}
  13. {{var_dump($data2)}}
  14. {{var_dump($data1)}}
  15. </div>

insert方法插入返回值为布尔值,一次可以同时插入多条记录,insertGetId方法插入并返回 ID 值,一次只能插入一条记录

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