You can create a class to hold the fields, and validation, then call a function to render it with a single template. You can write this as a library so can be reused. Here is an idea of what you can do.
class TableInterface
{
public function getFields();
}
abstract class TableAbstract
{
private $model;
private $attribute;
public function render()
{
$attribute = $this->attribute;
return view('universal.table.template', [
'fields' => $this->getFields(),
'data' => $this->model->$attribute
]);
}
public static function create($model, $attribute)
{
$table = new self();
$table->model = $model;
$table->attribute = $attribute;
return $table;
}
}
class AddressTable extends TableAbstract implments TableInterface
{
public function getFields()
{
return [
'street' => [
'label' => 'Street'
'validation' => [
// may be rules
]
]
];
}
}
You can call this as follows:
echo AddressTable::create(User::find(1), 'addresses')->render();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community