Skip to content

Instantly share code, notes, and snippets.

@Tomanow
Last active February 5, 2016 12:28
Show Gist options
  • Save Tomanow/cd795632ed94f52bf0ec to your computer and use it in GitHub Desktop.
Save Tomanow/cd795632ed94f52bf0ec to your computer and use it in GitHub Desktop.
Laravel Table View Helper with Model Action Links
<?php
/**
* .-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.
* =================================================== *
* Class TableHelper
* --------------------------------------------------- *
* Description:
* A Laravel View Table Creation Helper
* Includes action links for show, edit & delete
* --------------------------------------------------- *
* Instructions:
* Provide array of model objects ($users)
* and array of $allowed fields in format
* array('field_name' => 'Table Header Label')
* then, wrap with <table>.
* EXAMPLE:
<?php
$allowed = array(
'id' => 'ID',
'name' => 'Name',
'description' => 'Description',
'created_at' => 'Created',
'updated_at' => 'Updated'
);
$extras = array(
'model' => 'user'
); ?>
<table class="make-datatable table table-responsive table-striped table-bordered" id="user-table">
{!! TableHelper::createTable($users, $allowed, $extras) !!}
</table>
* --------------------------------------------------- *
* @author Tom W. DeBerardine <tom@tomanow.com>
* @link https://www.tomanow.com Author's Site
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0
* =================================================== *
*/
class TableHelper
{
public static function createHeaderRow($obj, $allowed, $extras = array())
{
$attributes = $obj[0]['attributes'];
$html = '<thead><tr>';
foreach ($allowed as $attribute => $nice_name) {
if (array_key_exists($attribute, $attributes)) {
$html .= '<th>' . $nice_name . '</th>';
}
}
if (array_key_exists('model', $extras)) {
$html .= '<th>Actions</th>';
}
$html .= '</tr></thead>';
return $html;
}
public static function createTableBody($obj, $allowed, $extras = array())
{
$html = '<tbody>';
foreach ($obj as $key => $value) {
$html .= '<tr>';
foreach ($allowed as $fkey => $field) {
$html .= '<td>' . $value->$fkey . '</td>';
}
if (array_key_exists('model', $extras)) {
$html .= TableHelper::createLinks($extras['model'], $value);
}
$html .= '</tr>';
}
$html .= '</tbody>';
return $html;
}
public static function createLinks($model_name, $obj)
{
$html = '<td>';
$html .= '<a href="' . route($model_name . '.' . 'edit', [$obj]) . '">' . 'Edit' . '</a>&nbsp;';
$html .= '<a href="' . route($model_name . '.' . 'show', [$obj]) . '">' . 'Show' . '</a>';
$html .= \Form::open(array('url' => route($model_name . '.' . 'destroy', [$obj]), 'method' => 'delete'));
$html .= '<button type="submit" class="btn btn-danger btn-mini" onclick="
if (!confirm(\'Delete this ' . $model_name . ' object (id ' . $obj->id . ')?\')) { return false; }"><i class="glyphicon glyphicon-trash"></i> Delete</button>';
$html .= \Form::close();
$html .= '</td>';
return $html;
}
public static function createTable($obj, $allowed, $extras = array())
{
return TableHelper::createHeaderRow($obj, $allowed, $extras) . TableHelper::createTableBody($obj, $allowed, $extras);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment