加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Laravel 数据库操作 Eloquent ORM

发布时间:2020-12-14 19:59:01 所属栏目:大数据 来源:网络整理
导读:div class="cnblogs_code" 建立模型 use IlluminateDatabaseEloquentModel; class Student extends Model { //指定表名 默认 模型名的复数 protected $table='student'; //指定主键 默认主键 为IDprotected $primaryKey='id';//指定允许批量赋值的字段pro

<div class="cnblogs_code">

建立模型
use IlluminateDatabaseEloquentModel;

class Student extends Model
{
//指定表名 默认 模型名的复数
protected $table='student';

//指定主键 默认主键 为ID
protected  $primaryKey='id';

//指定允许批量赋值的字段
protected $fillable=['name','age'];
//指定不允许批量赋值的字段
protected $guarded=[];

//是否维护时间戳  默认维护
//$timestamps=falst 不维护
public $timestamps=true;

//维护时间的时候保存时间戳
protected function getDateFormat()
{
    return time(); // TODO: Change the autogenerated stub
}

//查询的时候返回时间戳
protected function asDateTime($value)
{

// return parent::asDateTime($value); // TODO: Change the autogenerated stub
return $value;
}
}

  

ORM 查询
=Student:: </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;根据主键查询 查询一条数据</span> <span style="color: #800080"&gt;$student</span>=Student::find(2<span style="color: #000000"&gt;); dd(</span><span style="color: #800080"&gt;$student</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;findOrFail() 根据主键查询 如果没有查到 报错</span> <span style="color: #800080"&gt;$student</span>=Student::findOrFail(2<span style="color: #000000"&gt;); dd(</span><span style="color: #800080"&gt;$student</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;get() 查询所有数据</span> <span style="color: #800080"&gt;$students</span>=Student::<span style="color: #000000"&gt;get(); dd(</span><span style="color: #800080"&gt;$students</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;first() 查询第一条</span> <span style="color: #800080"&gt;$students</span>=Student::where('id','>','1')->orderBy('age','desc')-><span style="color: #000000"&gt;first(); dd(</span><span style="color: #800080"&gt;$students</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;chunk() 每次查询一定条数 </span> Student::chunk(2,<span style="color: #0000ff"&gt;function</span>(<span style="color: #800080"&gt;$students</span><span style="color: #000000"&gt;){ </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$students</span><span style="color: #000000"&gt;); }); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;聚合函数 //count() 条数</span> <span style="color: #800080"&gt;$num</span> = Student::<span style="color: #008080"&gt;count</span><span style="color: #000000"&gt;(); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$num</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;max() 查询最大值</span> <span style="color: #800080"&gt;$max</span>=Student::where('id',1)-><span style="color: #008080"&gt;max</span>('age'<span style="color: #000000"&gt;); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$max</span>);</pre>
添加
=->name='vbb'->age=34=-> </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;create()</span> <span style="color: #800080"&gt;$rs</span>=Student::<span style="color: #000000"&gt;create([ </span>'name'=>'momo','age'=>23<span style="color: #000000"&gt; ]); dd(</span><span style="color: #800080"&gt;$rs</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;firstOrCreate()以属性查询数据 如果没有 新建数据</span> <span style="color: #800080"&gt;$rs</span>=Student::<span style="color: #000000"&gt;firstOrCreate( [</span>'name'=>'vbb4'<span style="color: #000000"&gt;] ); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;firstOrNew() 以属性查询数据 如果没有 新建实例 如果想保存调用save()</span> <span style="color: #800080"&gt;$rs</span>=Student::<span style="color: #000000"&gt;firstOrNew( [</span>'name'=>'vbb4'<span style="color: #000000"&gt;] ); </span><span style="color: #800080"&gt;$bool</span>=<span style="color: #800080"&gt;$rs</span>-><span style="color: #000000"&gt;save(); dd(</span><span style="color: #800080"&gt;$rs</span>);</pre>
更新
=Student::find(2->age=2=->( </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;批量更新</span> <span style="color: #800080"&gt;$num</span>=Student::where('id',5)-><span style="color: #000000"&gt;update( [</span>'age'=>41<span style="color: #000000"&gt;] ); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$num</span>);</pre>
删除
=Student::find(2=->( </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;通过主键删除</span> <span style="color: #800080"&gt;$num</span>=Student::destroy(3,4,5<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$num</span>=Student::destroy([3,5<span style="color: #000000"&gt;]); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$num</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;删除指定条件</span> <span style="color: #800080"&gt;$num</span>=Student::where('id',7)-><span style="color: #000000"&gt;delete(); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$num</span>);</pre>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读