Laravel之备忘项(不定期更新)
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之备忘项(不定期更新)的更多相关文章
- laravel相关备忘
此次笔记采用的是laravel5.1版本 1.从gitcheckout下来后,首先在env修改数据库相关 2.默认laravel没有model目录,默认有一个model文件User.php放在app里 ...
- git备忘(长久更新)
一直想了解一下git,正好最近的有一个问题就是,实验室写的代码,怎么同步到自己宿舍的笔记本上面来.最开始想用dropbox,但是用VS的人都知道,工程文件里面会给你生成乱七八糟的很多东西,很占空间,d ...
- 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 ...
- Linux 命令备忘(持续更新中……)
Linux命令 grep 1. 使用grep 筛选内容,多条件筛选用 grep - E "条件1|条件2" (满足条件1或条件2的均展示) 2. grep '条件3'|grep - ...
- laravel知识点备忘
1.连表查询:select * from goods left join shop on goods.shopid=shop.shopid; DB::table('goods') ->leftJ ...
- mac指令备忘
在这里简单记录下最近使用的快捷键,备忘,随时更新. 简单指令记录 mkdir 创建路径 pwd 输出当前路径 ls 查看目录 cd touch 创建文件 tree 输出目录树 mv 源文件 目标文件或 ...
- [转载]备忘:oh my zsh 的安装、更新、删除
备忘:oh my zsh 的安装.更新.删除 傅易君 关注 0.8 2016.09.25 00:56* 字数 68 阅读 14920评论 0喜欢 4 查看系统当前 shell $ cat /etc/ ...
- 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)
本文记录一些关于Verilog HDL的一些技巧.易错.易忘点等(主要是语法上),一方面是方便自己忘记语法时进行查阅翻看,另一方面是分享给大家,如果有错的话,希望大家能够评论指出. 关键词: ·技巧篇 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
随机推荐
- Data_Structure01-绪论作业!
一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...
- Winform 模拟Session
背景 在Web中Session的功能很好用,于是想Winform中实现该功能,典型应用场景则是登陆成功后,当一段时间不操作,则该会话过期,提示重新登陆. 资源下载 测试代码 示例说明:登陆进去10s不 ...
- pywordfrom
http://files.cnblogs.com/files/zhang-pengcheng/pywordform-0.02.zip Win8.1自带微软五笔输入法开启方法
- 总结DSP28335的程序设计的方法
对DSP进行开发时,需要对其底层的硬件及外设进行相应的配置,当配置完成后才可以将其相应模块激活,才可以在其内部进行程序编写及调试处理.下面对程序配置及操作进行简单的整理,仅供参考. 第一步:初始化系统 ...
- MTK_GPIO口的定制
http://blog.csdn.net/zuoyioo7/article/details/77863291如果需要定制GPIO口呢,需要使用mediatek/dct/DrvGen.exe工具,点击O ...
- POCO库中文编程参考指南(4)Poco::Net::IPAddress
POCO库中文编程参考指南(4)Poco::Net::IPAddress 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmai ...
- UVA 11925:Generating Permutations(冒泡排序 Grade D)
VJ题目链接 题意:n个数(n<300),是一个1~n的某个排列.有两种操作:操作1把前两个数换位置,操作2把第一个数移动到最后.问给出一个排列,问通过怎样的操作,能把1,2,3,...,n变到 ...
- corosync基本使用
相关rpm: corosync-2.4.0-4.el6.x86_64.rpm The Corosync Cluster Engine and Application Programming Inter ...
- git提交代码出现错误fatal: Unable to create '项目路径/.git/index.lock': File exists.
git提交代码出现错误fatal: Unable to create '项目路径/.git/index.lock': File exists. 具体出错代码如下: 具体原因不详,在stackoverf ...
- 利用JavaScript打印出Fibonacci数(不使用全局变量)
从汤姆大叔的博客里看到了6个基础题目:本篇是第4题 - 利用JavaScript打印出Fibonacci数(不使用全局变量) 解题关键: 1.Fibonacci数列的规律 2.递归 解点1:Fibon ...