laravel的model
2024-10-07 05:39:21
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();
- orderBy的意思是排序。
- desc是倒序。
- 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());
最新文章
- 在PowerShell中使用curl(Invoke-WebRequest)
- T-SQL Recipes之Dynamic PIVOT and UNPIVOT
- cmd运行的程序的工作目录
- 树莓派3B安装pybluz
- InnoDB源码分析--事务日志(一)
- Vue.js组件示例
- python数据分析之pandas库的DataFrame应用一
- Discuz DB层跨库映射关系表名前缀BUG修复后产生的新bug
- 转载RabbitMQ入门(5)--主题
- Python 学习日记(第四周)
- js中的call()、apply()和bind()方法的区别
- Eclipse详细设置护眼背景色和字体颜色并导出
- gnome 3 美化
- Azure ARM虚拟机部署反恶意软件-安全扩展
- SharePoint 2013 引发类型为“System.ArgumentException”的异常。 參数名: encodedValue
- SQL Server-聚焦ROW_NUMBER VS TOP N性能
- Matlab图像处理常用基本函数
- android 实现点击edittext的“小眼睛”切换明密文
- 【druid 】数据库连接池-sql解析
- iperf网络测试
热门文章
- spark 笔记 3:Delay Scheduling: A Simple Technique for Achieving Locality and Fairness in Cluster Scheduling
- MySQL 数据库 常用函数
- Docker-----版本选择
- leetcode 347前k个高频元素
- ColorPicker 颜色选择器
- python之拷贝文件
- [转] javascript 正则表达式提取数字使用
- 【ABAP系列】SAP ABAP7.40新语法简介第一篇
- django.db.migrations.exceptions.BadMigrationError: Migration tests in app bl
- Shell编程、part4