Blogger Information
Blog 14
fans 0
comment 1
visits 12725
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel构造器及模型
王珂
Original
658 people have browsed it

laravel构造器及模型

home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Article;
  6. // 后台主页
  7. class Home extends Controller{
  8. // 查询多条记录
  9. // get()
  10. // get()->all()数组
  11. public function list(){
  12. $res = DB::table('article')->get()->all();
  13. echo '<pre>';
  14. print_r($res);
  15. }
  16. // 指定查询字段select
  17. // 条件where('cate_id','=',5), '='可省略
  18. // and使用where()->where()
  19. // or使用where()->orWhere()
  20. public function selectlist(){
  21. $res = DB::table('article')->select('cate_id as cid','title')->where('cate_id',5)->get()->all();
  22. echo '<pre>';
  23. print_r($res);
  24. }
  25. // like查询
  26. // tosql可查看sql语句,?是点位符
  27. public function likes(){
  28. $res = DB::table('article')->where('title','like','%5%')->where('cate_id',5)->tosql();
  29. echo '<pre>';
  30. print_r($res);
  31. }
  32. // where in查询
  33. public function wherein(){
  34. $res = DB::table('article')->whereIn('cate_id',[1,5,7])->get()->all();
  35. echo '<pre>';
  36. print_r($res);
  37. print_r($res[0]->title);
  38. }
  39. // 连表查询
  40. public function joins(){
  41. $res = DB::table('article')->join('users','users.id','=','article.uid')->select('article.title','users.username as nickname')->get()->all();
  42. echo '<pre>';
  43. print_r($res);
  44. }
  45. // 平均avg,最大max,最小min,计数count
  46. public function pvs(){
  47. $res = DB::table('article')->avg('pv');
  48. //$res = DB::table('article')->max('pv');
  49. //$res = DB::table('article')->min('pv');
  50. //$res = DB::table('article')->count();
  51. $res = (int)$res;
  52. echo '<pre>';
  53. print_r($res);
  54. }
  55. // 增加数据
  56. public function insert2(){
  57. $data = [];
  58. $data[]= array('uid'=>2,'cate_id'=>7,'title'=>'insert测试5','pv'=>0);
  59. $data[]= array('uid'=>1,'cate_id'=>6,'title'=>'insert测试4','pv'=>0);
  60. $res = DB::table('article')->insert($data);
  61. echo '<pre>';
  62. var_dump($res);
  63. }
  64. // 增加数据并获取ID
  65. public function insert3(){
  66. $data= array('uid'=>1,'cate_id'=>8,'title'=>'insert测试7','pv'=>0);
  67. $res = DB::table('article')->insertGetId($data);
  68. echo '<pre>';
  69. var_dump($res);
  70. }
  71. // 修改数据
  72. public function update2(){
  73. $res = DB::table('article')->whereIn('id',[10,11])->update(array('title'=>'update测试'));
  74. echo '<pre>';
  75. var_dump($res);
  76. }
  77. //删除
  78. public function delete2(){
  79. $res = DB::table('article')->whereIn('id',[10,12])->delete();
  80. echo '<pre>';
  81. var_dump($res);
  82. }
  83. //模型
  84. public function mymodels(Article $article){
  85. $res = $article->get()->toArray();
  86. echo '<pre>';
  87. print_r($res);
  88. }
  89. }

创建模型(article.php)

在powershell中
H:\phpstudy_pro\www\laravel7> php artisan make:model Article

article.php

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Article extends Model
  5. {
  6. //指定表名,不然默认找名字为“articles”的表
  7. protected $table= 'article';
  8. }
Correction status:Uncorrected

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