模型
首先在App目录下建立student的文件夹 里面放上 Student.php 且需要继承基类Model
//允许批量赋值的字段
// protected $fillable = ['name','age'];
//不允许批量赋值的字段
// protected $guarded = ['name','age'];

//自动维护时间戳
public $timestamps = true;

控制器
Route::any('orm1','StudentController@orm1');//ORM查询
public function orm1()
{
//查询全部 All()
// $student = Student::All();
//查询单个ID find()
// $student = Student::find(11);
//findOrFail() 根据主键查找如果没查到就报错
// $student = Student::findOrFail(11);
//get()获取所有数据
// $student = Student::get()->toArray();
//whereRaw()复合条件查询
// 查询年龄为18且名字为gwz的一条字段 $student = Student::whereRaw('name = ? and age = ?',['gwz',18])->first()->toArray();

}
Route::any('orm2','StudentController@orm2');//ORM增加
//ORM增加
public function orm2()
{
//新增属性与值
// $student = new Student();
// $student->name = 'guowe2nzheng';
// $student->age = '223';
// $bloon = $student->save();
// dd($bloon);

// 查找主键id为$id的时间戳
// $student = Student::find(18);
// echo $student->created_at;

// 添加
// $student = Student::insert(
// ['name'=>'nbsp','age'=>18]
// );
// dd($student);

// firstOrCreate()
//firstOrCreate() 如果有则查出这个属性 如果没有 则增加这个属性 需要用的 Model中的protected $fillable = ['name'];
// $student = Student::firstOrCreate([
// 'name'=>'gwz12321'
// ]);
// dd($student);

//firstOrNew() 如果有则查 没有则创建实例 需要用的 Model中的protected $fillable = ['name'];
// $student = Student::firstOrNew([
// 'name'=>'123'
// ]);
// $student->save();
// dd($student);
}
Route::any('orm3','StudentController@orm3');//ORM修改
//ORM修改
public function orm3()
{
// $student = Student::find(22);//查出模型
// $student->name = 'kituy';//赋值属性
// $student->save();//执行修改
// $num = Student::where('id',22)->update(['age'=>31]);//修改id为22的 age 字段
// var_dump($num);
}
Route::any('orm4','StudentController@orm4');//ORM删除
//ORM删除
public function orm4()
{
// $student = Student::find(22);//查出要删除的模型
// $student->delete();//执行删除方法

// $num = Student::destroy(20,19);//直接删除主键id的当前记录返回影响行数

// Student::where('id',18)->delete();//删除id为18的记录

}

Laravel数据库操作 Eloquent ORM的更多相关文章

  1. Laravel 数据库操作 Eloquent ORM

    laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...

  2. Laravel 学习笔记之数据库操作——Eloquent ORM

    1. 时间戳 默认情况下在使用ORM操作数据库进行添加.修改数据时, created_at 和 updated_at列会自动存在于数据表中,并显示的是 ‘2017’格式,如果想以 Unix时间戳格式存 ...

  3. [转]Laravel 4之Eloquent ORM

    Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...

  4. Laravel—数据库操作与Eloquent模型使用总结

    数据库操作 执行原生SQL //查询 $emp = DB::select('select * from employees where emp_no = 1'); $emp = DB::select( ...

  5. [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)

    简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...

  6. laravel 数据库操作

    1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...

  7. laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM

    <?php namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class S ...

  8. Laravel 数据库操作之Eloquent ORM模型

    //模型中的相关代码 namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model{ //默认对 ...

  9. laravel数据库操作sql语句用Eloquent ORM来构造

    现在有查询语句: SELECT users.sNmame, users.iCreateTime, users_ext.iAge, users_ext.sSex FROM users LEFT JOIN ...

随机推荐

  1. NSIS脚本 打包安装程序

    相关工具 nsis http://nsis.sourceforge.net/Special_Builds HM NIS Edit http://hmne.sourceforge.net/ 例子: ; ...

  2. cin与cout格式化输出

    cin 中“<<”流 输入运算符 ----------------------------------------------------------------------------- ...

  3. 网易2018校招内推编程题-堆棋子-C++实现

    链接:https://www.nowcoder.com/questionTerminal/27f3672f17f94a289f3de86b69f8a25b来源:牛客网 [编程题]堆棋子 热度指数:14 ...

  4. centos7 安装php7

    方法一 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https:/ ...

  5. [转]Golang TLS

    首先是自签证书: openssl与数字证书的使用 https://blog.csdn.net/yue7603835/article/details/72569012 Golang TLS服务端/客户端 ...

  6. Python数据类型的可变与不可变

    首先,我们需要知道在python中哪些是可变数据类型,哪些是不可变数据类型.可变数据类型:列表list和字典dict:不可变数据类型:整型int.浮点型float.字符串型string和元组tuple ...

  7. 全志A33 lichee 搭建Qt App开发环境编写helloworld

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Step 1 在虚拟机(Ce ...

  8. 创建一个dynamics 365 CRM online plugin (一) - Hello World Plugin

    源代码连接:https://github.com/TheMiao/Dynamics365CRM/blob/master/MyCRM/MyCRM/HelloWorld.cs 首先,我们需要创建一个.NE ...

  9. day052 django第三天 url和视图

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...

  10. 哈密顿绕行世界问题、n皇后问题

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...