博客列表 >laravel数据库访问--php培训九期线上班

laravel数据库访问--php培训九期线上班

取个名字真难
取个名字真难原创
2020年01月02日 15:05:27620浏览

、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 值,一次只能插入一条记录

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