Blogger Information
Blog 35
fans 3
comment 0
visits 25088
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月31日larave 作业
随风
Original
693 people have browsed it

增删改查及insert和insertGetId的不同

` public function sel3()
{
$data = DB::table(‘article’)->get()->toArray();

  1. return view('article.article3',['res'=>$data]);
  2. }
  3. public function myadd1()
  4. {
  5. $data = DB::table('article')
  6. ->insert(['title'=>'构造器插入数据','fb_date'=>'2019-12-31',
  7. 'soure'=>'北京','ydl'=>'22','pls'=>'33','image'=>'b.gif',
  8. 'detail'=>'构造器插入数据','cate_id'=>'3']);
  9. print_r($data);
  10. return view('article.article',['res'=>$data]);
  11. }
  12. // 插入 构造器
  13. public function myadd2()
  14. {
  15. $data = DB::table('article')
  16. ->insert([
  17. ['title'=>'构造器插入数据多条记录1','fb_date'=>'2019-12-31',
  18. 'soure'=>'北京','ydl'=>'22','pls'=>'33','image'=>'b.gif',
  19. 'detail'=>'构造器插入数据','cate_id'=>'3'],
  20. ['title'=>'构造器插入数据多条记录2','fb_date'=>'2019-12-31',
  21. 'soure'=>'北京','ydl'=>'22','pls'=>'33','image'=>'b.gif',
  22. 'detail'=>'构造器插入数据','cate_id'=>'3']
  23. ]);
  24. print_r($data);
  25. return view('article.article',['res'=>$data]);
  26. }
  27. // 插入 构造器
  28. public function myadd3()
  29. {
  30. $data = DB::table('article')
  31. ->insertGetId(
  32. ['title'=>'构造器插入数据自动获得id','fb_date'=>'2019-12-31',
  33. 'soure'=>'北京','ydl'=>'22','pls'=>'33','image'=>'b.gif',
  34. 'detail'=>'构造器插入数据','cate_id'=>'3']
  35. );
  36. print_r($data);
  37. return view('article.article',['res'=>$data]);
  38. }
  39. // 修改 构造器
  40. public function upd1()
  41. {
  42. $data =DB::table('article')
  43. ->where('article_id',31)

// ->update([‘title’=>’构造器修改数据’]);
->update([‘title’=>’构造器修改数据’,’detail’=>’构造器修改数据’]);

  1. print_r($data);
  2. return view('article.article',['res'=>$data]);
  3. }
  4. // 删除 构造器
  5. public function mydel1()
  6. {
  7. $data =DB::table('article')
  8. ->where('article_id','34')
  9. ->delete();
  10. print_r($data);
  11. return view('article.article', ['res' => $data]);
  12. }`

其他查询

` public function myfile()
{
$res =DB::table(‘article’)
->select(‘article_id’,’title’,’fb_date’,’soure’,’ydl’,’pls’,’image’,’detail’,’cate_id’)
->where(‘article_id’,’>’,10)
->whereIn(‘cate_id’,[2,3])
->whereBetween(‘article_id’,[17,30])
->orderBy(‘article_id’,’desc’)
->get()
->toArray();

  1. return view('dbtest.test',['data'=>$res]);
  2. }
  3. public function myjh()
  4. {
  5. $res =DB::table('article')
  6. ->count();
  7. echo '<pre>';
  8. echo $res;
  9. echo '<br>';
  10. $res1 =DB::table('article')
  11. ->max('article_id');
  12. print_r($res1);
  13. echo '<br>';
  14. $res2 =DB::table('article')
  15. ->select(DB::raw('cate_id,Sum(pls),count(*)'))
  16. ->groupBy('cate_id')
  17. ->get();
  18. print_r($res2);
  19. echo '<br>';
  20. return view('dbtest.test1',['data'=>$res]);
  21. }
  22. public function mjoins()
  23. {
  24. $res = 0;
  25. $res = DB::table('article')
  26. ->join('category','article.cate_id','=','category.cate_id')
  27. ->select('article.*','category.name')
  28. ->whereIn('article_id',[29,30])
  29. ->get();
  30. echo '<pre>';
  31. print_r($res);echo '<br>';
  32. return view('dbtest.test1',['data'=>$res]);
  33. }`

总结

insert和insertGetId的不同

一个是返回值为1,一个是返回为自增长的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