zoukankan      html  css  js  c++  java
  • Laravel Eloquent ORM 数据model操作

    Laravel ORM 数据model操作

     
    注意:ORM关联操作最后一定要记得带get()方法!否则会获取不到数据,为null
    如:   posts表      comments表 
         id        id
        contents     post_id
                comment
     
      post模型内定义一对多关系hasMany
      
    1     //定义关联关系
    2    public function comments(){
    3         return $this->hasMany(Comments::class,'post_id','id');
    4     }
    1 //容器内调用模型获得数据, $id 为Posts中主键id
    2 public function getComments($id){
    3       return Posts::find($id)->comments()->get();  
    4 }
     
    1.ORM操作需要创建对应的model
            class User extends Eloquent
     
    2.有两种方式使用数据操作对象
               a. 使用new关键字创建对象后执行对象的方法
               b. 直接调用static方法(实际并发静态方法,而是fascade生成的)
     
    3.常用数据操作
           a.  User::find(1)    查找单条数据
           b.  User::all()        查找所有数据
           c.   User::find(1)->delete()    删除单条数据
           d.    User::destory(array(1,2,3))    删除单条或多条数据
           e.    User::save()        保存数据
            f.    User::first()        取第一条数据
           g.    Album::where('artist''=''Matt Nathanson'->update(array('artist' => 'Dayle Rees'));    指定查询条件,更新数据
            h.  User::truncate()    清空数据表,危险操作
            i.   Album::where('artist''=''Something Corporate')->get(array('id','title'));    配合查询条件获取多条数据
           j. Album::pluck('artist');            返回表中该字段的第一条记录
           k. Album::lists('artist');                返回一列数据
           l.   Album::where('artist''=''Something Corporate')->toSql();     获取查询的sql语句,仅用于条件,不能用户带get()之类的带查询结果的查询中
    注:直接使用return 查询结果为json格式的数据
           这里使用的User为model名称
     
    条件查询:
       1. 最普通的条件查询 User::where('字段名','查询字符','限制条件')     例:Album::where('title', 'LIKE', '...%')
       2. 多条件查询,使用多个where    Album::where('title', 'LIKE', '...%')->where('artist', '=', 'Say Anything')->get();
       3. 或查询操作使用orWhere(),使用方法通where
        4.直接用sql语句写查询条件    Album::whereRaw('artist = ? and title LIKE ?', array('Say Anything', '...%'))
        5. 其他查询方法
           whereIn(),whereBetween(),whereNested()子查询,orWhereNested(),whereNotIn(),whereNull(),whereNotNull()
        6. 快捷方式  whereUsername('king')  查询'username' = 'king'的数据,默认系统无此方法,username为字段名称
     
    结果排序:
      使用order关键字:
         Album::where('artist''=''Matt Nathanson')->orderBy('year')->get();   默认asc
        orderBy('year''desc')
     
    限制结果数量
        take()方法
        Album::take(2)->get();                          //select * from `albums` limit 2
     
    指定偏移
        Album::take(2)->skip(2)->get();        //select * from `albums` limit 2 offset 2
  • 相关阅读:
    JS中encodeURIComponent在PHP中实现的办法_tdweb的博客,仅仅是个博客_百度空间
    废弃的java 爬虫代码
    c#实现Javascript的encodeURIComponent()函数
    Encode query with Python
    Chunked decoding in python
    python implemention javascript encodeURIComponent
    Java MongoDB : Insert a document
    tar zcvf ./xxx.tar.gz ./directory
    MyStringUtils test
    application/xwwwformurlencoded
  • 原文地址:https://www.cnblogs.com/ggkxg/p/7380308.html
Copyright © 2011-2022 走看看