• <bdo id='J5hsN'></bdo><ul id='J5hsN'></ul>

      <legend id='J5hsN'><style id='J5hsN'><dir id='J5hsN'><q id='J5hsN'></q></dir></style></legend>
    1. <small id='J5hsN'></small><noframes id='J5hsN'>

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

      <tfoot id='J5hsN'></tfoot>

        如何在 Laravel 4 中获取列名?

        How do I get column names in Laravel 4?(如何在 Laravel 4 中获取列名?)

        <small id='C3jK6'></small><noframes id='C3jK6'>

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

            <tfoot id='C3jK6'></tfoot>

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

                • 本文介绍了如何在 Laravel 4 中获取列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在 Laravel 4 中使用 Schema、DB 或 Eloquent 获取数组或对象中表的列名?

                  How can I get column names of a table in an array or object in Laravel 4, using Schema, DB, or Eloquent?

                  我好像找不到一个现成的函数,也许你有一些自定义的实现.

                  It seems that I can't find a ready to use function, maybe you have some custom implementations.

                  推荐答案

                  新答案

                  当时我给出了这个答案Laravel 没有办法直接做到这一点,但现在你可以:

                  $columns = Schema::getColumnListing('users');
                  

                  旧答案

                  使用属性是行不通的,因为如果你这样做了

                  Old Answer

                  Using attributes won't work because if you do

                  $model = new ModelName;
                  

                  您没有为该模型设置任何属性,您将一无所获.

                  You have no attributes set to that model and you'll get nothing.

                  那么仍然没有真正的选择,所以我不得不进入数据库级别,这是我的 BaseModel:

                  Then there is still no real option for that, so I had to go down to the database level and this is my BaseModel:

                  <?php
                  
                  class BaseModel extends Eloquent {
                  
                      public function getAllColumnsNames()
                      {
                          switch (DB::connection()->getConfig('driver')) {
                              case 'pgsql':
                                  $query = "SELECT column_name FROM information_schema.columns WHERE table_name = '".$this->table."'";
                                  $column_name = 'column_name';
                                  $reverse = true;
                                  break;
                  
                              case 'mysql':
                                  $query = 'SHOW COLUMNS FROM '.$this->table;
                                  $column_name = 'Field';
                                  $reverse = false;
                                  break;
                  
                              case 'sqlsrv':
                                  $parts = explode('.', $this->table);
                                  $num = (count($parts) - 1);
                                  $table = $parts[$num];
                                  $query = "SELECT column_name FROM ".DB::connection()->getConfig('database').".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'".$table."'";
                                  $column_name = 'column_name';
                                  $reverse = false;
                                  break;
                  
                              default: 
                                  $error = 'Database driver not supported: '.DB::connection()->getConfig('driver');
                                  throw new Exception($error);
                                  break;
                          }
                  
                          $columns = array();
                  
                          foreach(DB::select($query) as $column)
                          {
                              $columns[] = $column->$column_name;
                          }
                  
                          if($reverse)
                          {
                              $columns = array_reverse($columns);
                          }
                  
                          return $columns;
                      }
                  
                  }
                  

                  用它来做:

                  $model = User::find(1);
                  
                  dd( $model->getAllColumnsNames() );
                  

                  这篇关于如何在 Laravel 4 中获取列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

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

                            <tbody id='AifHF'></tbody>
                        • <small id='AifHF'></small><noframes id='AifHF'>

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