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('AppProduct');
    }
}
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 = AppPerson::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();

  

原文地址:https://www.cnblogs.com/itfenqing/p/6935198.html