Hello friends,
I have five tables and all of them have different number of columns. I want to create view for them so that user can input the values. But I don't want to create 5 different static views blade files. Is there any way by which depending on the table and number of column my view gets updated. and form gets generated? Is it possible?
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