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( ...
随机推荐
- RadioGroup和RadioButton(单选框)
1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- Python 求最大公因式~辗转相除法
从错误中学python(4)——最小公约数与辗转相除法 网上看到一篇很简洁辗转相除法的写法:不用判断a,b的大小 def gcp(a, b): while(b%a!=0): a,b=b%a,a ret ...
- calc(NOIP模拟赛Round 3)
原题: D e s c r i p t i o n 给三个正整数n,m和p,求(n^1+...n^m) mod p. Input 一行,三个整数n,m和p. Output 输出答案. S a m p ...
- Visual Studio Code 好用的 source code editor
short cut https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf go to definition : F1 ...
- MTK_GPIO口的定制
http://blog.csdn.net/zuoyioo7/article/details/77863291如果需要定制GPIO口呢,需要使用mediatek/dct/DrvGen.exe工具,点击O ...
- 配置和读取INI
#define MAX_FILE_PATH 260 void CControlDlg::OnBnClickedBtnGamepath() { // TODO: 在此添加控件通知处理程序代码 CFile ...
- (29)C#多线程
使用线程的原因 1.不希望用户界面停止响应. 2.所有需要等待的操作,如文件.数据库或网络访问需要一定的时间. 一个进程的多个线程可以同时运行不同cpu或多核cpu的不同内核上 注意多线程访问相同的数 ...
- (1)C#工具箱-公共控件1
公共控件 InitializeComponent() 先说下InitializeComponent()这个方法,它在form1.cs里调用这个方法对控件进行初始化,控件的方法要在这个方法之后,否则会因 ...
- Codeforces 581F Zublicanes and Mumocrates(树型DP)
题目链接 Round 322 Problem F 题意 给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...
- unittest (python标准库-开发工具-单元测试框架)
unittest官方文档摘录 翻译 reffer to: https://docs.python.org/3/library/unittest.html#unittest.TextTestRunner ...