我正在尝试使用质量分配Eloquent功能创建实体...
$new = new Contact(Input::all());
$new->save();
Run Code Online (Sandbox Code Playgroud)
问题是这样,每个字段用空字符串填充而不是null
我预期的值.
我正在开发系统,但仍然没有定义一些表列,这就是为什么使用这种方法,以避免将每个新字段添加到$fillable
数组和new Contact(array(...));
...
此外,我在这个表中有大约20个字段,所以有一个像这样的数组有点难看
$new = new Contact(array(
'salutation' => Input::get('salutation'),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'company_id' => Input::get('company_id'),
'city' => ...
...
));
Run Code Online (Sandbox Code Playgroud)
有关如何执行此操作或修复的任何提示?
更新到现在为止,我已经整理了App::before()
过滤器中的array_filter .
更新过滤器有点乱.我最终做了:
public static function allEmptyIdsToNull()
{
$input = Input::all();
$result = preg_grep_keys ( '/_id$/' , $input );
$nulledResults = array_map(function($item) {
if (empty($item))
return null;
return $item;
}, $result);
return array_merge($input, $nulledResults);
}
Run Code Online (Sandbox Code Playgroud)
在我的functions.php中.
if ( …
Run Code Online (Sandbox Code Playgroud)