1.自定义字段验证错误信息

 $this->validate($request,
['name' => 'required|max:50'],
['name.required' => '名称必须','name.max' => '名称长度不能大于:max']
);

  

2.简单打印sql语句

DB::connection()->enableQueryLog();
$user = User::find(1);
dd(DB::getQueryLog());

  

3.fill填充数组
有时候我们需要用一个数组来填充model,又希望返回bool值(create默认返回的是一个model实例)

$model->fill($array)->save();

  

4.getDirty获取受影响的属性
我们建立了model,并更新了属性,但在保存的时候,我们需要where判断,来防止数据已经被另一个用户更改,但save无法和where并用,只能使用update,可以使用getDirty获取受影响的属性作为数组传入update

$model = TestModel::first(1);
$model->name = 'test';
$model->where('version', $model->version)->update($model->getDirty());

  

5.update和save
如果模型不带where

$task = Task::first(1);
$task->update(['name' => 'test']); //返回bool值,这由上层的model类处理

  

如果带where

$task->where('id', 1)->update(['name' => 'test']); //这由下层builder类处理,返回受影响的记录数

  

6.where的另一种用法

$model->first($id);
$model->where('name', $name)->first();
$model->whereName($name)->first();

  

7.relation中的where

public function category()
{
return $this->belongsTo('myCategoryModel', 'categories_id')->where('users_id', Auth::user()->id);
}

  

8.日期过滤

$q->whereDate('created_at', date('Y-m-d'));
$q->whereDay('created_at', date('d'));
$q->whereMonth('created_at', date('m'));
$q->whereYear('created_at', date('Y'));

  

9.save保存模型实例的时候设置timestamps

$product = Product::find($id);
$product->updated_at = '2015 -01-01 10:00:00';
$product->save(['timestamps' => false]);

  

10.orderBy

$model->orderBy('id', 'asc')->get();

等同于

$model->orderBy('id')->get();

  

11.lists

$model->lists(field); //返回field的集合,集合中是一个field的数组,键名为数字
$model->lists(field1,field2); //返回field的集合,集合中是一个数组,键名为field2,值为field1

  

12.用关联来过滤主model中的数据

class Category extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
}
public function getIndex()
{
# 这条语句的意思是,渴求式记在products,并且Category中至少有1个产品
$categories = Category::with('products')->has('products')->get();
# 这条语句的意思是,渴求式记在products,并且Category中至少有3个产品
$categories = Category::with('products')->has('products', '>=', 3)->get();
return view('categories.index', compact('categories'));
}

  

13.保存数据的时候返回关联

public function getUser()
{
$task = new Task();
$task->user_id = 1;
$task->name = 'test';
$task->save();
return $task->user; // 这将返回一个id为1的user模型
}

  

14.模板赋值

view('posts.index')->with('posts', $posts);
//等同于
view('posts.index')->withPosts($posts);

  

15.模板中的第一条和最后一条处理

@foreach ($menu as $item)

<div @if ($item != reset($menu)) class="hidden" @endif>

<h2>{{ $item->title }}</h2>

     </div>
@endforeach @foreach ($menu as $item) <div @if ($item == end($menu)) class="no_margin" @endif> <h2>{{ $item->title }}</h2> </div> @endforeach

  

16.find

$collection = App\Person::find([1, 2, 3]);

  

17.where对集合的继续过滤

$tasks = Task::get();
$ceshi2 = $tasks->where('name', 'ceshi2');

  

18.where和lists结合使用

$first_name = $collection->where('meta_key', 'first_name')->lists('value')[0];

  

19.自定义错误

return response()->view('errors.default', ['exception' => $e], 500); //500是页面状态相应

  

20.模型初始化

$task = new Task(['name'=>'test']);
$task->save();

  

Laravel之备忘项(不定期更新)的更多相关文章

  1. laravel相关备忘

    此次笔记采用的是laravel5.1版本 1.从gitcheckout下来后,首先在env修改数据库相关 2.默认laravel没有model目录,默认有一个model文件User.php放在app里 ...

  2. git备忘(长久更新)

    一直想了解一下git,正好最近的有一个问题就是,实验室写的代码,怎么同步到自己宿舍的笔记本上面来.最开始想用dropbox,但是用VS的人都知道,工程文件里面会给你生成乱七八糟的很多东西,很占空间,d ...

  3. T-SQL备忘-表连接更新

    1.update a inner join b on a.id=b.id set a.name=b.name where ...   2.update table1 set a.name = b.na ...

  4. Linux 命令备忘(持续更新中……)

    Linux命令 grep 1. 使用grep 筛选内容,多条件筛选用 grep - E "条件1|条件2" (满足条件1或条件2的均展示) 2. grep '条件3'|grep - ...

  5. laravel知识点备忘

    1.连表查询:select * from goods left join shop on goods.shopid=shop.shopid; DB::table('goods') ->leftJ ...

  6. mac指令备忘

    在这里简单记录下最近使用的快捷键,备忘,随时更新. 简单指令记录 mkdir 创建路径 pwd 输出当前路径 ls 查看目录 cd touch 创建文件 tree 输出目录树 mv 源文件 目标文件或 ...

  7. [转载]备忘:oh my zsh 的安装、更新、删除

    备忘:oh my zsh 的安装.更新.删除 傅易君 关注  0.8 2016.09.25 00:56* 字数 68 阅读 14920评论 0喜欢 4 查看系统当前 shell $ cat /etc/ ...

  8. 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)

    本文记录一些关于Verilog HDL的一些技巧.易错.易忘点等(主要是语法上),一方面是方便自己忘记语法时进行查阅翻看,另一方面是分享给大家,如果有错的话,希望大家能够评论指出. 关键词: ·技巧篇 ...

  9. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

随机推荐

  1. 洛谷P1450 [HAOI2008]硬币购物

    题目描述 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. 输入输出格式 输入格式: 第一 ...

  2. http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/

    转载自:http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/ 推荐他的所有博文~ 图片 ...

  3. [bzoj2245][SDOI2011]工作安排——费用流

    题目大意: 传送门 题解: 很容易建模,把每一个工作人员拆成两个点,由第一个点向第二个点连S+1条边即可. 这水题没什么难度,主要是longlong卡的丧心病狂... 代码 #include < ...

  4. 我读过的最好的epoll讲解--转自”知乎“ 【转】

    转自:http://blog.csdn.net/xu3737284/article/details/12715963 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O ...

  5. system call hooking 系统调用增加或劫持

    1. 引言:这篇文章提供了一种增加自定义系统调用或劫持原有的系统调用的实现方法,只针对 linux 系统.主要思路是获取系统调用表 sys_call_table 地址,然后用新函数地址覆盖系统调用表某 ...

  6. (七)ubuntu下编译openwrt内核的环境配置

    首先安装基本开发环境: sudo apt-get install ssh vim samba tftp nfs 安装编译openwrt须要的包: 解压openwrt包编译出错: Build depen ...

  7. 第二步:开发工具Eclipse安装并汉化

    打开下载官网:www.eclipse.org.点击下载(download英文)然后就是安装步骤了,还是一样一直的点击下一步,默认安装到C盘.如下图: 汉化步骤: 1.打开www.eclipse.org ...

  8. 天猫首页迷思之-jquery实现整个div的懒加载(1)

    懒加载是众所周知的减少网页负载,提高性能的方法,不少大型用图片用的多的网站都用到了. 于是我网上一搜,得到一插件:jquery.lazyload    网址:http://www.appelsiini ...

  9. 树莓派3b入门教程

    原文地址:传送门 这篇教程将带您一起玩转树莓派3(Raspberry Pi 3).和普通PC一样,拿到新设备第一件事就是要给它安装一个操作系统,并做一些初始化的操作.比PC简单的是,树莓派是一个固定配 ...

  10. 树链剖分【p2590】[ZJOI2008]树的统计

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...