Hello
I would like to read database (sqlite) records from within a LayoutBuilder block in order to embed those database records on different pages together with other LayoutBuilder blocks. I am using a third party Layout Theme (implemented as Plugin) by extending this with my db block.
I created the block php file within site/theme/snippets/blocks and can access the database records and render the entries by :
$records = Db::select('projects');
“Unfortunately” in the rendering part “foreach($records as $record): ... $record->project_title() ...” I can not use in this context useful methods like ->html(), ->url() etc. thus $record->project_title()->html()
I tried also from the database guide to use part of the model code to create a virtual page structure like :
foreach (Db::select('projects') as $record) {
$records[] = [
'slug' => $record->project_id(),
'num' => 0,
# 'template' => 'comment',
# 'model' => 'comment',
'content' => [
'title' => $record->project_title(),
'text' => $record->project_web_description(),
'url' => $record->project_link()',
'uuid' => Uuid::generate(),
]
];
};
This represents an array but not a page/children object. I can not figure out how to to convert this array to a valid page/children object within the block code.
At the moment I am not sure if I have to implement a fully virtual page structure or I can go with the simple database access approach.
My aim is the following:
- a “general purpose” LayoutBuilder block with database access (read only) for different tables
- an appropriate block panel blueprint to define main parameters of the block like db “table”, db select parameters etc. and styling/rendering parameters.
- I do not want to show/edit the single records in the panel (as the database records a part of an external PM-system).
- I would like to use kirby tags, created on the fly with the database select/query statements (virtual db fields) to be able to filter records by tags (within the same block). Thus user should be able to filter records by clicking on the presented tags-list.
I would appreciate any hint for a suitable approach. Many thanks in advance.
André