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. ios手机弹出层上表单的操作,收起键盘焦点错乱的问题

    今天遇到了ios手机下 弹出层上form表单 当收起键盘后,焦点错乱,无法再操作的问题 解决办法 function device() { const u = navigator.userAgent; ...

  2. DataTable 去重合并

    //合并 dt.Merge(dt2); //去重 dt = dt.AsDataView().ToTable(true);

  3. [字符串] StartWith和EndWith效率比较低

    之前无意中看到有人提到StartWith和EndWith效率比较低,今天恰好有用到这样的场景,于是写了个测试验证一下. 该示例仅在比较字符串首尾单个字符,用途有限. var str = "\ ...

  4. 用vs2008和vs2005创建win32 console application

    http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...

  5. 1.tornado基础

    import tornado.web ''' tornado基础web框架模块 ''' import tornado.ioloop ''' tornado的核心循环IO模块,封装了linux的epol ...

  6. (30)C#Timer类

    有三种Timer 1.System.Windows.Forms.Timer 应用于WinForm中,它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程 ...

  7. 51nod 1087 1 10 100 1000【打表】

    题目来源: Ural 1209 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 1,10,100,1000...组成序列1101001000...,求 ...

  8. Hibernate使用Criteria去重distinct+分页

    写在前面: 最近在项目中使用了Criteria的分页查询,当查询的数据没有重复的记录还好,但是当数据有关联并出现重复记录的时候,就要去重,那么就会出现查询的记录数与实际的不一致的问题.这里也记录一下解 ...

  9. RMAN BACKUP

    转自 RMAN BACKUP backup terminology Using the RMAN BACKUP Command to Create Backups Server-Managed Con ...

  10. md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样

    md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样