• <i id='ckpQ5'><tr id='ckpQ5'><dt id='ckpQ5'><q id='ckpQ5'><span id='ckpQ5'><b id='ckpQ5'><form id='ckpQ5'><ins id='ckpQ5'></ins><ul id='ckpQ5'></ul><sub id='ckpQ5'></sub></form><legend id='ckpQ5'></legend><bdo id='ckpQ5'><pre id='ckpQ5'><center id='ckpQ5'></center></pre></bdo></b><th id='ckpQ5'></th></span></q></dt></tr></i><div id='ckpQ5'><tfoot id='ckpQ5'></tfoot><dl id='ckpQ5'><fieldset id='ckpQ5'></fieldset></dl></div>

  • <small id='ckpQ5'></small><noframes id='ckpQ5'>

      <bdo id='ckpQ5'></bdo><ul id='ckpQ5'></ul>
    <legend id='ckpQ5'><style id='ckpQ5'><dir id='ckpQ5'><q id='ckpQ5'></q></dir></style></legend>

    1. <tfoot id='ckpQ5'></tfoot>

        空字符串而不是空值 Eloquent

        时间:2023-11-01
        1. <small id='hlS4c'></small><noframes id='hlS4c'>

          <tfoot id='hlS4c'></tfoot>
              <bdo id='hlS4c'></bdo><ul id='hlS4c'></ul>

              • <legend id='hlS4c'><style id='hlS4c'><dir id='hlS4c'><q id='hlS4c'></q></dir></style></legend>
                • <i id='hlS4c'><tr id='hlS4c'><dt id='hlS4c'><q id='hlS4c'><span id='hlS4c'><b id='hlS4c'><form id='hlS4c'><ins id='hlS4c'></ins><ul id='hlS4c'></ul><sub id='hlS4c'></sub></form><legend id='hlS4c'></legend><bdo id='hlS4c'><pre id='hlS4c'><center id='hlS4c'></center></pre></bdo></b><th id='hlS4c'></th></span></q></dt></tr></i><div id='hlS4c'><tfoot id='hlS4c'></tfoot><dl id='hlS4c'><fieldset id='hlS4c'></fieldset></dl></div>
                    <tbody id='hlS4c'></tbody>

                  本文介绍了空字符串而不是空值 Eloquent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用批量分配 Eloquent 功能创建实体...

                  I'm trying to create entities using mass-assignment Eloquent feature...

                  $new = new Contact(Input::all());
                  $new->save();
                  

                  问题在于,这样一来,每个字段都填充了一个空字符串,而不是我预期的 null 值.

                  The problem's that this way, every field's filled out with an empty string instead of null values as I expected.

                  我目前正在开发系统,但仍有一些表列没有定义,这就是为什么使用这种方法,以避免将每个新字段添加到 $fillable 数组和 new联系人(array(...));...

                  I'm currently developing the system and still some table columns're not defined, that's why using this method, to avoid adding every new field to $fillable array and to a new Contact(array(...));...

                  此外,我在这个表中有大约 20 个字段,所以有一个这样的数组有点难看

                  Also I've around 20 fields in this table, so It'd be a bit ugly to have an array such as

                  $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' => ...
                      ...
                  ));
                  

                  有关如何执行此操作或修复的任何提示?

                  Any tips of how to do this or fix?

                  更新到目前为止,我已经在 App::before() 过滤器中解决了这个问题.

                  Update By now I've sorted out this doing the array_filter in the App::before() filter.

                  更新过滤器有点乱.我最终做了:

                  Update In filter was a bit mess. I end up doing:

                  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);
                  }
                  

                  在我的functions.php中.

                  And in my functions.php.

                  if ( ! function_exists('preg_grep_keys'))
                  {
                      /**
                      * This function gets does the same as preg_grep but applies the regex
                      * to the array keys instead to the array values as this last does.
                      * Returns an array containing only the keys that match the exp.
                      * 
                      * @author Daniel Klein
                      * 
                      * @param  string  $pattern
                      * @param  array  $input
                      * @param  integer $flags
                      * @return array
                      */
                      function preg_grep_keys($pattern, array $input, $flags = 0) {
                          return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags)));
                      }
                  }
                  

                  现在只使用以_id"结尾的字段.这是我最大的问题,如果关系不是NULL,数据库会因为找不到外键"而抛出错误.

                  By now only working with fields that ends with "_id". This is my biggest problem as if a relationship is not NULL, the database will throw an error as the foreign key "" cannot be found.

                  完美运行.有什么意见吗?

                  Works perfect. Any comment?

                  推荐答案

                  使用模型保存"事件查找空模型并将其显式设置为 null.在此示例中,项目"是我的模型的名称.把它放在一个辅助函数中并将它连接到你的所有模型.

                  Use the model 'saving' event to look for empty models and explicitly set them to null. 'Project' is the name of my model in this example. Put this in a helper function and wire it up to all your models.

                  Project::saving(function($model) {
                      foreach ($model->toArray() as $name => $value) {
                          if (empty($value)) {
                              $model->{$name} = null;
                          }
                      }
                  
                      return true;
                  });
                  

                  LARAVEL 5 更新(2016 年 4 月 11 日)

                  UPDATE FOR LARAVEL 5 (April 11 2016)

                  我最后还创建了一个 Http 中间件来清理来自 http 请求的输入数据,除了在数据发送到数据库之前清理数据.

                  I also ended up creating an Http Middleware to cleanup the input data from the http request, in addition to cleaning up the data before it is sent to the database.

                  class InputCleanup
                  {
                      /**
                       * Handle an incoming request.
                       *
                       * @param  IlluminateHttpRequest  $request
                       * @param  Closure  $next
                       * @return mixed
                       */
                      public function handle($request, Closure $next)
                      {
                          $input = $request->input();
                  
                          array_walk_recursive($input, function(&$value) {
                  
                              if (is_string($value)) {
                                  $value = StringHelper::trimNull($value);
                              }
                          });
                  
                          $request->replace($input);
                  
                          return $next($request);
                      }
                  }
                  

                  这篇关于空字符串而不是空值 Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“laravel.log"无法打开:无法打开流 下一篇:从控制器调用外部 API 函数,LARAVEL 4

                  相关文章

                    <small id='1mChU'></small><noframes id='1mChU'>

                    <legend id='1mChU'><style id='1mChU'><dir id='1mChU'><q id='1mChU'></q></dir></style></legend>
                  1. <i id='1mChU'><tr id='1mChU'><dt id='1mChU'><q id='1mChU'><span id='1mChU'><b id='1mChU'><form id='1mChU'><ins id='1mChU'></ins><ul id='1mChU'></ul><sub id='1mChU'></sub></form><legend id='1mChU'></legend><bdo id='1mChU'><pre id='1mChU'><center id='1mChU'></center></pre></bdo></b><th id='1mChU'></th></span></q></dt></tr></i><div id='1mChU'><tfoot id='1mChU'></tfoot><dl id='1mChU'><fieldset id='1mChU'></fieldset></dl></div>

                        <bdo id='1mChU'></bdo><ul id='1mChU'></ul>
                      <tfoot id='1mChU'></tfoot>