1.创建模型

$ php artisan make:model Models/Issue
 

2.模型的白名单机制,用于赋值

class Issue extends Model
{
  
  //指定表名
  protected $table = 'article2';   //指定主键
  protected $primaryKey = 'article_id';
  
  
  //是否开启时间戳
  protected $timestamps = false;   //设置时间戳格式为Unix
  protected $dateFormat = 'U';
  
  //过滤字段,只有包含的字段才能被更新
  protected $fillable = ['title','content'];   //隐藏字段
  protected $hidden = ['password'];
}
class Issue extends Model
{
protected $fillable = ['title'];
}

3.向模型填充数据

$ php artisan tinker
use App\Models\Issue

Issue::create(['title' => 'PHP Lover'])
Issue::create(['title' => 'Rails and Laravel'])
Issue::all()

4.从模型读取数据

use App\Models\Issue;

$issues = Issue::orderBy('created_at', 'desc')
->take()
->get();
  1. orderBy的意思是排序。
  2. desc是倒序。
  3. take(2)是只读取两条数据。
 

5.添加一个资源

use App\Models\Issue;

Issue::create($request->all());

6.删除一个资源

use App\Models\Issue;

Issue::destroy($id);

7.修改一个资源

use App\Models\Issue;

$issue = Issue::find($id);
$issue->update($request->all());

最新文章

  1. 在PowerShell中使用curl(Invoke-WebRequest)
  2. T-SQL Recipes之Dynamic PIVOT and UNPIVOT
  3. cmd运行的程序的工作目录
  4. 树莓派3B安装pybluz
  5. InnoDB源码分析--事务日志(一)
  6. Vue.js组件示例
  7. python数据分析之pandas库的DataFrame应用一
  8. Discuz DB层跨库映射关系表名前缀BUG修复后产生的新bug
  9. 转载RabbitMQ入门(5)--主题
  10. Python 学习日记(第四周)
  11. js中的call()、apply()和bind()方法的区别
  12. Eclipse详细设置护眼背景色和字体颜色并导出
  13. gnome 3 美化
  14. Azure ARM虚拟机部署反恶意软件-安全扩展
  15. SharePoint 2013 引发类型为“System.ArgumentException”的异常。 參数名: encodedValue
  16. SQL Server-聚焦ROW_NUMBER VS TOP N性能
  17. Matlab图像处理常用基本函数
  18. android 实现点击edittext的“小眼睛”切换明密文
  19. 【druid 】数据库连接池-sql解析
  20. iperf网络测试

热门文章

  1. spark 笔记 3:Delay Scheduling: A Simple Technique for Achieving Locality and Fairness in Cluster Scheduling
  2. MySQL 数据库 常用函数
  3. Docker-----版本选择
  4. leetcode 347前k个高频元素
  5. ColorPicker 颜色选择器
  6. python之拷贝文件
  7. [转] javascript 正则表达式提取数字使用
  8. 【ABAP系列】SAP ABAP7.40新语法简介第一篇
  9. django.db.migrations.exceptions.BadMigrationError: Migration tests in app bl
  10. Shell编程、part4