1. Go to this page and download the library: Download tkeer/flattable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tkeer / flattable example snippets
public function getFlattableConfig(): array
{
[
[
'columns' => [
//flattable column => 'source model column'
'name' => 'name',
'published_at' => 'published_at',
'publisher_id' => 'publisher_id',
'book_id' => 'id'
],
// type of relationship b/w flattable and model
'type' => 'primary',
// how to find related entry in the flattable table
'wheres' => [
// key is flattable column
// value is column of source table (book)
'book_id' => 'id',
],
'flattable' => 'books_flattable',
]
]
}
public function getFlattableConfig(): array
{
[
[
'flattable' => 'books_flattable',
...
'changes' => [
// foreign colum name
// we will update changes data only if this column is update(dirty)
'publisher_id' => [
'columns' => [
'publisher_first_name' => 'first_name',
'publisher_last_name' => 'last_name',
],
// talbe name of the source
'table' => 'publishers',
]
]
]
]
}
public function flattableConfig(): array
{
...
return [
[
'columns' => [
'id' => 'id',
'name' => 'name'
],
// use type many when you want to store more than one entry in a column
'type' => 'many',
'wheres' => [
'publisher_id' => 'publisher_id',
],
//only delete from old if these keys have changed
'delete_from_old_keys' => ['publisher_id'],
'flattable' => 'publishers_flattable',
// column name of the flaatable, in which the data should be stored.
'flattable_column_name' => 'books',
]
]
}