Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is nothing like this in <code>Laravel</code> AFAIK but you may create your own, something like (an idea only) this (a <code>php</code> class, not only for <code>Laravel</code>)</p> <pre><code>class Table { protected $table = null; protected $header = null; protected $attr = null; protected $data = null; public function __construct($data = null, $attr = null, $header = null) { if(is_null($data)) return; $this-&gt;data = $data; $this-&gt;attr = $attr; if(is_array($header)) { $this-&gt;header = $header; } else { if(count($this-&gt;data) &amp;&amp; $this-&gt;is_assoc($this-&gt;data[0]) || is_object($this-&gt;data[0])) { $headerKeys = is_object($this-&gt;data[0]) ? array_keys((array)$this-&gt;data[0]) : array_keys($this-&gt;data[0]); $this-&gt;header = array(); foreach ($headerKeys as $value) { $this-&gt;header[] = $value; } } } return $this; } public function build() { $atts = ''; if(!is_null($this-&gt;attr)) { foreach ($this-&gt;attr as $key =&gt; $value) { $atts .= $key . ' = "' . $value . '" '; } } $table = '&lt;table ' . $atts . ' &gt;'; if(!is_null($this-&gt;header)) { $table .= '&lt;thead&gt;&lt;tr&gt;'; foreach ($this-&gt;header as $value) { $table .= '&lt;th&gt;' . ucfirst($value) . '&lt;/th&gt;'; } $table .= '&lt;/thead&gt;&lt;/tr&gt;'; } $table .= '&lt;tbody&gt;'; foreach ($this-&gt;data as $value) { $table .= $this-&gt;createRow($value); } $table .= '&lt;/tbody&gt;'; $table .= '&lt;/table&gt;'; return $this-&gt;table = $table; } protected function createRow($array = null) { if(is_null($array)) return false; $row = '&lt;tr&gt;'; foreach ($array as $value) { $row .= '&lt;td&gt;' . $value . '&lt;/td&gt;'; } $row .= '&lt;/tr&gt;'; return $row; } protected function is_assoc($array){ return is_array($array) &amp;&amp; array_diff_key($array, array_keys(array_keys($array))); } } </code></pre> <p>Now, you can use it as given below (<a href="http://codepad.viper-7.com/dnJXru" rel="nofollow noreferrer"><strong><strike>An Example Here.</strike></strong></a>)</p> <pre><code>$data = array( array('name' =&gt; 'Heera', 'age'=&gt;'35', 'address' =&gt;'Masimpur', 'phone'=&gt;'123456'), array('name' =&gt; 'Usman', 'age'=&gt;'28', 'address' =&gt;'Kamal Gor', 'phone'=&gt;'654321') ); $attr = array('class'=&gt;'tbl someClass', 'id'=&gt;'myTbl', 'style'=&gt;'width:400px;color:red', 'border'=&gt;'1'); $t = new Table($data, $attr); echo $t-&gt;build(); </code></pre> <p>Or, set an header using third argument, like</p> <pre><code>$t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact')); </code></pre> <p>This is just an idea and could be better. Now just integrate this class with <code>Laravel</code> using <code>Laravel</code>'s rule. You may extend <code>Html</code> class or use as an individual class by registering it as a service. Take a look at <a href="https://stackoverflow.com/questions/14629083/laravel-extending-class/14629223#14629223">this answer</a> for extending a class in <code>laravel</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload