Laravel数据库操作 Eloquent ORM
模型
首先在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的更多相关文章
- Laravel 数据库操作 Eloquent ORM
laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...
- Laravel 学习笔记之数据库操作——Eloquent ORM
1. 时间戳 默认情况下在使用ORM操作数据库进行添加.修改数据时, created_at 和 updated_at列会自动存在于数据表中,并显示的是 ‘2017’格式,如果想以 Unix时间戳格式存 ...
- [转]Laravel 4之Eloquent ORM
Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...
- Laravel—数据库操作与Eloquent模型使用总结
数据库操作 执行原生SQL //查询 $emp = DB::select('select * from employees where emp_no = 1'); $emp = DB::select( ...
- [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)
简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...
- laravel 数据库操作
1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...
- laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM
<?php namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class S ...
- Laravel 数据库操作之Eloquent ORM模型
//模型中的相关代码 namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model{ //默认对 ...
- laravel数据库操作sql语句用Eloquent ORM来构造
现在有查询语句: SELECT users.sNmame, users.iCreateTime, users_ext.iAge, users_ext.sSex FROM users LEFT JOIN ...
随机推荐
- 阶段01Java基础day23多线程
23.01_多线程(多线程的引入) 1.什么是线程 线程是程序执行的一条路径, 一个进程中可以包含多条线程 多线程并发执行可以提高程序的效率, 可以同时完成多项工作 2.多线程的应用场景 红蜘蛛同时共 ...
- java基础知识点学习
基础学习总结 1.锁sync/lock都有哪些方法,底层实现 synchronized ['sɪŋkrənaɪzd] 2.线程池的参数.线程池执行的流程,当到达线程池到达最大数,队列也满了,出现的异常 ...
- Python编程--类的分析
一.类的概念 python是面向对象的编程语言,详细来说,我们把一类相同的事物叫做类,其中用相同的属性(其实就是变量描述),里面封装了相同的方法,比如:汽车是一个类,它包括价格.品牌等属性.那么我们如 ...
- LAMP架构上(一)
第十七课LAMP架构上(一) 目录 一.LAMP架构介绍 二.MySQL.MariaDB介绍 三.MySQL安装 四.MariaDB安装 五.Apache安装 六.安装PHP5 七.安装PHP7 八. ...
- 清楚理解const_cast类型转换
1.任何使用原常量的地方, 已经直接编码到代码中去了.故后续转换类型并不能改变原定义 2.const_cast转换, 是使用了新指针或者引用,指向了原定义的内存,故而可以修改该内存. 使用也得用新指针 ...
- Post Order traverse binary tree using non-recursive way
Process analysis Stack = 5, Push 3, Stack = 5, 3. Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...
- 《CSS揭秘》之毛玻璃效果知识点
实现代码: CodePen:毛玻璃效果 Dabblet:毛玻璃效果 HTML: <main> <blockquote> <em>"The only way ...
- 仿QQ菜单栏:消息,电话菜单
转载自:http://blog.csdn.net/johnnyz1234/article/details/45919907 在实际项目开发使用Fragment的时候,也碰到一些异常和存在的问题,下面做 ...
- Windows7 密码修改
一:不用输入原密码的方式修改用户的密码 1 命令行输入命令:mmc #进入到控制台 2 点击左上角的文件,选择添加/删除管理单元 3 选择本地用户和组管理单元,添加到本地计算机,完成,确定 4 添加 ...
- 使用outflux 导入influxdb 的数据到timescaledb
influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...