关键词搜索

源码搜索 ×
×

PHP笔记-laravel框架中Eloquent ORM实现增删改查

发布2022-05-16浏览785次

详情内容

表结构是这样的:

  1. /*
  2. SQLyog Ultimate v12.09 (64 bit)
  3. MySQL - 5.7.12 : Database - laravel
  4. *********************************************************************
  5. */
  6. /*!40101 SET NAMES utf8 */;
  7. /*!40101 SET SQL_MODE=''*/;
  8. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  9. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  10. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  11. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  12. CREATE DATABASE /*!32312 IF NOT EXISTS*/`laravel` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
  13. USE `laravel`;
  14. /*Table structure for table `student` */
  15. DROP TABLE IF EXISTS `student`;
  16. CREATE TABLE `student` (
  17. `id` int(11) NOT NULL AUTO_INCREMENT,
  18. `name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  19. `age` int(11) DEFAULT NULL,
  20. PRIMARY KEY (`id`)
  21. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  22. /*Data for the table `student` */
  23. insert into `student`(`id`,`name`,`age`) values (2,'小张',19),(3,'小明',18),(4,'小张',19);
  24. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  25. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  26. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  27. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

源码如下:

路由web.php添加:

  1. Route::prefix('student')->group(function(){
  2. ......
  3. ......
  4. Route::get('ormInsert', 'StudentController@ormInsert');
  5. Route::get('ormDelete', 'StudentController@ormDelete');
  6. Route::get('ormModify', 'StudentController@ormModify');
  7. Route::get('ormSelect', 'StudentController@ormSelect');
  8. });

这个config/database.php是数据库相关的设置,目前不用改,对应mysql是这样的


需要修改的是

.env

修改为如下:

  1. DB_CONNECTION=mysql
  2. DB_HOST=127.0.0.1
  3. DB_PORT=3306
  4. DB_DATABASE=laravel
  5. DB_USERNAME=root
  6. DB_PASSWORD=root

新建Student.php

代码如下:

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Student extends Model {
  5. // 指定表名
  6. protected $table = 'student';
  7. // 指定ID
  8. protected $primaryKey = 'id';
  9. // 自动维护时间戳
  10. public $timestamps = true;
  11. // 指定允许批量赋值的字段
  12. protected $fillable = ['name', 'age'];
  13. // 指定不允许批量赋值的字段
  14. protected $guarded = [];
  15. // protected function getDateFormat(){
  16. //
  17. // return time();
  18. // }
  19. //
  20. // protected function asDateTime($val){
  21. //
  22. // return $val;
  23. // }
  24. }

新建StudentController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Student;
  4. use Illuminate\Support\Facades\DB;
  5. class StudentController extends Controller{
  6. ......
  7. ......
  8. ......
  9. public function ormInsert(){
  10. $student = new Student();
  11. $student->name = '小红';
  12. $student->age = 19;
  13. $bool = $student->save();
  14. // dd($bool);
  15. $student = Student::create(
  16. ['name' => '小张', 'age' => 20]
  17. );
  18. // dd($student);
  19. //firstOrCreate()
  20. $student = Student::firstOrCreate(
  21. ['name' => '小王', 'age' => 32]
  22. );
  23. // dd($student);
  24. //firstOrNew这个需要手动保存
  25. $student = Student::firstOrNew(
  26. ['name' => '小黑', 'age' => 33]
  27. );
  28. $bool = $student->save();
  29. dd($bool);
  30. return "ormInsert";
  31. }
  32. public function ormDelete(){
  33. // 通过模型删除
  34. $student = Student::find(1);
  35. $bool = $student->delete();
  36. var_dump($bool);
  37. // 通过主键删除
  38. $num = Student::destroy(2);
  39. var_dump($num);
  40. $num = Student::destroy([2, 3]);
  41. var_dump($num);
  42. // 条件删除
  43. $num = Student::where('id', '>', 10)->delete();
  44. var_dump($num);
  45. return "ormDelete";
  46. }
  47. public function ormModify(){
  48. $student = Student::find(1);
  49. $student->name = 'kitty';
  50. $bool = $student->save();
  51. var_dump($bool);
  52. $num = Student::where('id', '>', 5)->update(
  53. ['age' => 38]
  54. );
  55. var_dump($num);
  56. return "ormModify";
  57. }
  58. public function ormSelect(){
  59. $student = Student::find(1);
  60. dd($student);
  61. return "ormSelect";
  62. }
  63. }

 

 

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载