读写分离之多个读?
有 'host' => $readHosts[array_rand($readHosts)],
上面的好像有缓存问题php artisan config:cache
法二可以链式调用
可以在AppServiceProvider 里面监听每一条sql语句
事务
DB::transaction(function () { DB::table('users')->update(['votes' => 1]); DB::table('posts')->delete();// 删除记录, 如果要清空表并将自动递增置为0,用truncate });
$users = DB::table('users')->get(); 可以链式调用,最终用get获取结果
$client = DB::table('clients')->where('email', 'client1@qq.com')->get();
DB::table('clients')->count();
只返回部分字段
->select('name', 'email as user_email')
union合并查询时,第一个不能用get()
whereDate, whereDay等可以直接对默认的created_at字段进行查询
inRandomOrder 将查询结果随机排序
插入记录并获取其 ID: insertGetId
更新
$affected_rows = DB::table('clients')
->where('id', 3)
->update(['name' => 'client3',
'email'=> 'client3@qq.com'
]);
分页
return DB::table('clients')->paginate(2);
App\User::paginate(15);
current_page:1 当前页码
"from": 1, 这个应该是本次返回的数据id范围
"to": 2, "last_page": 25, 总页数 "next_page_url": "http://laravel_web.app/test_select?page=2", "path": "http://laravel_web.app/test_select", "per_page": 2, 每页面显示的记录数 "prev_page_url": null, "total": 50 总记录条数
第2页面:
"current_page": 2,
"from": 3,
"to": 4, "last_page": 25, "next_page_url": "http://laravel_web.app/test_select?page=3", "path": "http://laravel_web.app/test_select", "per_page": 2, "prev_page_url": "http://laravel_web.app/test_select?page=1", "total": 50
{{ $users->fragment("ts=".time())->links() }}
软删除
1,use SoftDeletes;
protected $dates = ['deleted_at'];
2,数据库里加字段deleted_at
$table->softDeletes();
全局作用域允许我们为给定模型的所有查询添加条件约束。
Eloquent 模型会触发许多事件
如created,updated,deleted
关联关系
一对一hasOne, 是不是可以将方法名改为hasOnePhone
hasOne(类名, Phone里面的外键名称, 本表的主键id)
注意调用的时候不要加函数的括号!!!
查出来的是phone表的一条记录
一对多:
$users = Client::find(1)->hasManyPhones;
$users = Phone::find(1)->belongsToUser->name;
还可以链式调用,此时要加上括号
$users = Client::find(1)->hasManyPhones()->where('id', 2)->first();
一对多的反向关联跟一对一的反向关联是一样的。
多对多:
role_user 表命名是以相关联的两个模型数据表来依照字母顺序命名,如果不是这种,就要通过 第2个参数传递你的表名。如下:
belongsToMany ,第2个参数是关联表的名称,
第三个参数是本类的 id,第四个参数是第一个参数那个类的 id。
反向多对多:同上
远层一对多:Has Many Through 这个就是多级多层关联
countries id - integer users id - integer country_id - integer posts id - integer user_id - integer
虽然 posts 本身不包含 country_id 字段,但 hasManyThrough 关联通过 $country->posts 来让我们可以访问一个国家的文章。
return $this->hasManyThrough('App\Post', 'App\User');
第一个参数为我们希望最终访问的模型名称,而第二个参数为中间模型的名称。
第三个参数为中间模型的外键名称country_id ,而第四个参数为最终模型的外键名称user_id ,第五个参数则为本地键id。(好像一般最后一个参数都是id)
多态关联
一个模型在单个关联中从属一个以上其它模型
如:可以「评论」文章和视频,可以使用一个 comments 数据表就可以同时满足两个使用场景
posts id - integer videos id - integer comments id - integer commentable_id - integer commentable_type - string
commentable_id用于存放文章或者视频的 id ,而 commentable_type 用于存放所属模型的类名
通过$comment->commentable;可以反向取回Post或Video的实例。
多态多对多
如,博客的 Post 和 Video 模型可以共用多态关联至 Tag 模型
posts id - integer videos id - integer tags id - integer taggables tag_id - integer taggable_id - integer taggable_type - string
morphToMany('App\Tag', 'taggable');
参数:第一个参数为要关联的model名, 第2个参数为tag 里面的方法?
// 获取那些至少拥有一条评论的文章...用has $posts = App\Post::has('comments')->get();
// 获取所有至少有三条评论的文章... $posts = Post::has('comments', '>=', 3)->get();
关联数据计数
$posts = App\Post::withCount('comments')->get();
foreach ($posts as $post) { echo $post->comments_count; }
预加载
$books = App\Book::all(); foreach ($books as $book) { echo $book->author->name; }
若存在着 25 本书,则会执行 26 次查找:1 次是查找所有书籍,其它 25 次则是在查找每本书的作者。可以使用预加载来将查找的操作减少至 2 次。
$books = App\Book::with('author')->get();
对于该操作则只会执行两条 SQL 语句:
select * from books select * from authors where id in (1, 2, 3, 4, 5, ...)
延迟预加载
$books = App\Book::all(); if ($someCondition) { $books->load('author', 'publisher'); }
$comment = new App\Comment(['message' => 'A new comment.']); $post = App\Post::find(1); $post->comments()->save($comment);
最好不用Create 方法,需要设置批量赋值。
而且好像也要默认值,不然没有默认值的字段,如果不能批量赋值,sql语句会报错。
protected $attributes = [ 'name' => 'value' ];
更新用associate
多对多保存用attach,
更新中间表记录updateExistingPivot
修改器mutators
当 Eloquent 尝试获取 first_name 的值时,将会自动调用访问器:
当我们尝试在模型上设置 first_name 的值时,将会自动调用此修改器
自动转换
protected $casts = [ 'is_admin' => 'boolean', ];
临时修改属性的可见度
return $user->makeVisible('attribute')->toArray();
return $user->makeHidden('attribute')->toArray();
也可以添加一个在数据库中没有对应字段的属性
去掉created_at 和 updated_at 字段
public $timestamps = false;
生成手机号
$phone_prefix_arr = array(
130,131,132,133,134,135,136,137,138,139,
144,147,
150,151,152,153,155,156,157,158,159,
176,177,178,
180,181,182,183,184,185,186,187,188,189,
);
'phone' => $phone_prefix_arr[array_rand($phone_prefix_arr)].mt_rand(1000,9999).mt_rand(1000,9999),
- laravel学习之路1:认证相关
Laravel中Auth::guard()表示什么意思? Auth::check() 是判断用户是否登录的方法,如果使用的默认用户系统,那这样使用没问题. 但是使用两组用户的话,如何使用各组用户的功能 ...
- laravel学习之路4artisan
php artisan list php artisan help migrate Tinker 让你可以在命令行中与 Laravel 应用进行交互php artisan tinker 在routes ...
- laravel学习之路2: jwt集成
"tymon/jwt-auth": "^1.0@dev", 执行 composer update 'providers' => [ .... Tymon\ ...
- laravel学习之路5缓存
redis需要先安装 需要通过 Composer 安装 predis/predis 扩展包 (~1.0) 或者使用 PECL 安装 PhpRedis PHP 拓展. composer require ...
- 微软企业库5.0 学习之路——第二步、使用VS2010+Data Access模块建立多数据库项目
现在我就开始进入学习之路的第二步——Data Access模块,这个模块是企业库中被使用频率最高的模块,它很好的封装了数据库操作应用,为我们进行多数据库系统开发提供了便利,只需更改配置文件就 可以很快 ...
- Qt 学习之路 2(73):Qt 线程相关类
Home / Qt 学习之路 2 / Qt 学习之路 2(73):Qt 线程相关类 Qt 学习之路 2(73):Qt 线程相关类 豆子 2013年11月26日 Qt 学习之路 2 7条评论 希 ...
- Qt 学习之路 2(58):编辑数据库外键
Qt 学习之路 2(58):编辑数据库外键(skip) 豆子 2013年7月12日 Qt 学习之路 2 13条评论 前面几章我们介绍了如何对数据库进行操作以及如何使用图形界面展示数据库数据.本章我们将 ...
- Qt 学习之路 2(57):可视化显示数据库数据
Qt 学习之路 2(57):可视化显示数据库数据(skip) 豆子 2013年6月26日 Qt 学习之路 2 26条评论 前面我们用了两个章节介绍了 Qt 提供的两种操作数据库的方法.显然,使用QSq ...
- Qt 学习之路 2(56):使用模型操作数据库
Qt 学习之路 2(56):使用模型操作数据库 (okgogo: skip) 豆子 2013年6月20日 Qt 学习之路 2 13条评论 前一章我们使用 SQL 语句完成了对数据库的常规操作,包括简单 ...
随机推荐
- Windows 定时删除指定路径下N天前的日志文件
Windows 定时删除指定路径下N天前的日志文件 Windows 下bat脚本文件的内容为 1. 删除指定路径下5天前的所有文件 @echo off set SrcDir=E:\WORK\Git s ...
- 在ArcGIS Desktop中进行三参数或七参数精确投影转换
转自 在ArcGIS Desktop中进行三参数或七参数精确投影转换 ArcGIS中定义的投影转换方法,在对数据的空间信息要求较高的工程中往往不能适用,有比较明显的偏差.在项目的前期数据准备工 ...
- Python命令行参数学习
man python 查看python的帮助文件 命令行参数: -B Don't write .py[co] files on import. See a ...
- lamp+nginx代理+discuz+wordpress+phpmyadmin
实验课题:搭建LAMP,安装Nginx,作为代理,将MySQL安装在单独的机器,apache负责动态,nginx负责静态 实验环境: 1.VMware Workstation 11 2.设备A:MyS ...
- 关于并发,异步,非阻塞(python)疑惑的一些资料解答
从iterable/iterator到generator到coroutine理解python的迭代器: http://python.jobbole.com/81916/理解python的生成器: ht ...
- leetcode题解:Search for a Range (已排序数组范围查找)
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- python读写文件write和flush
打开文件用open,该函数创建一个文件对象,这将用来调用与之关联的其他支持方式. file object = open(file_name [, access_mode][, buffering]) ...
- 【Hadoop】Hadoop MR 自定义分组 Partition机制
1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...
- asp.net显示评论的时候为几天前的格式
自己做的一个小项目实现的功能,做个记录先~ 效果如图: 代码如下: public static class TimerHelper { public static string GetTimeSpan ...
- Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch 创建Client有几种方式. 首先在 Elasticsearch 的配置文件 elasticsearch.yml中.定义cluster.name.如下: cluster ...