thinkphp5单条批量添加数据和过滤非数据表字段

如果需要过滤非数据表字段的数据,可以使用:

$user=new User($_POST);

//过滤post数组中的非数据表字段数据

$user->allowField(true)->save();

如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:

$user=new User($_POST);

//post数组中只有name和email字段会写入

$user->allowField(['name','email'])->save();

create方法:给create方法传递第二个参数,true

User::create($_POST,true);    //加了true  会自动帮你调用allowField这个功能,只有有的字段才赋值,没有的字段过滤

练习:

猜你喜欢

转载自www.cnblogs.com/renshen/p/13196575.html