zoukankan      html  css  js  c++  java
  • laravel 数据操作

    一、基本概念和概述

    ① StdClass 对象=>基础的对象
    ② Eloquent 模型对象(Model 对象)=>和模型相关的类对象
    ③ Eloquent 集合=>可以简单理解为对象数组,里面的每一个元素都是一个Model 对象
    ④ 普通查询构造器返回的是StcClass对象或者是由基础对象组成的数组
    ⑤ Eloquent ORM返回的是 Eloquent对象(和模型相关的)或者是由模型对象组成的集合

    二、创建 Mould

    代码如下

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    
    class Loan extends Model
    {
        protected $fillable = [];
    
        protected $hidden = [];
    
        /**
         * Controller  与模型关联的表
         * author      Jack
         * version     2018/12/22  14:39
         */
        protected $table = 'loan';
    
        /**
         * Controller  指示是否对模型进行时间戳。
         * author      Jack
         * version     2018/12/22  14:39
        */
        public $timestamps = false;
    
        /**
         * Controller  模型的日期列的存储格式。
         * author      Jack
         * version     2018/12/22  14:54
        */
        protected $dateFormat = 'U';
    
    }

    三、Eloque 常用数据操作

     A 查询

    1) Customer::first(); ① 返回值:对象 ② 返回表中第一条数据

    2) Customer::find(1); ① 返回值:对象 ② 返回表中指定id的数据 ③ 必传参数 id

    3) Customer::all(); ① 返回值:集合 ② 返回表中所有数据 ③ 集合里面的每一个元素都是一个对象

    4) Customer::get(); ① 返回值:集合 ② 返回表中所有数据 ③ 集合里面的每一个元素都是一个对象

    B 删除

    5) Customer::find(2)->delete(); ① 返回值:truefalse ② 删除指定id的单条数据 ③ 必传参数 id

    6) Customer::destroy([1,2]); ① 返回值:删除条数 ②删除指定id的单条或多条数据 ③ 必传参数 id

    7) Customer::where('id', '>', 1)->delete(); ① 返回值:删除条数

    C 保存

    8) $customer->save()   

    9) Customer::insert(array(array(''=>,''=>),......)  ① 返回值:truefalse ② 参数类型数组

    D 修改

    10) Customer::where('id', '>', 10)->update(['seller_id'=>3]);① 返回值:修改条数

    四、可以了解操作

    ① Customer::truncate()                                             清空数据表
    ② Customer::where('id', '=', '1')->get(array('name','mobile'));    配合查询条件获取多条数据
    ③ Customer::pluck('mobile');            返回表中该字段的第一条记录
    ④ Customer::lists('artist');            返回一列数据 
    ⑤ Customer::where('mobile', '=', '1308744081')->toSql();     获取查询的sql语句,仅用于条件,不能用户带get()之类的带查询结果的查询中

    注:直接使用return 查询结果为json格式的数据这里使用的User为model名称

     A 最普通的条件查询 Customer::where('字段名','查询字符','限制条件')     

     eg:Customer::where('name', 'LIKE', '...%')

     B 多条件查询,使用多个where   

     eg: Customer::where('name', 'LIKE', '...%')->where('text', '=', '中国')->get();

     C 或查询操作使用orWhere(),使用方法通where

     D 直接用sql语句写查询条件    

     eg:Customer::whereRaw('name= ? and title LIKE ?', array('中国', '...%'))
     E 其他查询方法
     whereIn(),whereBetween(),whereNested()子查询,orWhereNested(),whereNotIn(),whereNull(),whereNotNull() 

     F 使用order关键字:

      eg:Customer::where('sex', '=', 1)->orderBy('age')->get();   默认asc

      orderBy('year', 'desc')

     G 限制结果数量 take()方法

    eg:Customer::take(2)->get();                          
    相当于select * from customer limit 2
    
    
    
    本人开发小白,所写随笔有转发、有心得、随笔、所见问题、或者感觉不错的东西,希望能帮助他人,同时也相当给自己方便!(未来及标明出处,望原作者以及读者见谅海涵!一切为了能解决问题。。。。)
  • 相关阅读:
    ArcEngine 图层标注 (根据字段、角度)
    以Graphicslayer为管理组来管理Element.
    ServletContextAware、ServletRequestAware、ServletResponseAware、SessionAware
    web.xml配置错误页面,及输出错误信息
    ServletContextListener使用详解
    自定义异常类。
    Hibernate注解与JPA
    Spring事务管理-<tx:advice>标签
    Spring配置之OpenSessionInViewFilter
    巧用Ajax的beforeSend 提高用户体验
  • 原文地址:https://www.cnblogs.com/chengjiao/p/10161786.html
Copyright © 2011-2022 走看看