itstructure / laravel-detail-view
Detail view table for Laravel: show an Eloquent model, an array or an object as a table of attributes and values
Package info
github.com/itstructure/laravel-detail-view
pkg:composer/itstructure/laravel-detail-view
Requires
- php: >=7.1.0
- laravel/framework: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^6.5|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Introduction
This package shows data of one record as a table: a row for each attribute, with a label and a value. The data can be an Eloquent model, an array or an object.
It works with any CSS framework: the package does not add its own classes, you set them in the config. For example table table-bordered table-striped for Bootstrap 4 or 5.
Requirements
- laravel 5.5+ | 6+ | 7+ | 8+ | 9+ | 10+ | 11+ | 12+ | 13+
- php >= 7.1
- composer
The package has no other dependencies.
Installation
General from remote packagist repository
Run the composer command:
composer require itstructure/laravel-detail-view "^1.1.0"
If you are testing this package from a local server directory
In application composer.json file set the repository, as in example:
"repositories": [ { "type": "path", "url": "../laravel-detail-view", "options": { "symlink": true } } ],
Here,
../laravel-detail-view - directory path, which has the same directory level as application and contains Detail View package.
Then run command:
composer require itstructure/laravel-detail-view:dev-main --prefer-source
Publish files (Not necessary)
-
To publish views run command:
php artisan detail_view:publish --only=viewsIt stores view files to
resources/views/vendor/detail_viewfolder. -
To publish translations run command:
php artisan detail_view:publish --only=langIt stores translation files to
resources/lang/vendor/detail_viewfolder. -
To publish all parts run command without
onlyargument:php artisan detail_view:publishElse you can use
--forceargument to rewrite already published files.
Else you can use --force argument to rewrite already published file.
Usage
View template part
Use @detailView() directive with config array in a blade view template.
Simple quick usage
You can simply set rows to display as string format in rowFields array.
Note: $model can be an Eloquent model (Illuminate\Database\Eloquent\Model), an array or an object. See Arrays and objects below.
@php $detailData = [ 'model' => $model, 'title' => 'Detail table', 'rowFields' => [ 'id', 'active', 'icon', 'created_at' ] ]; @endphp
@detailView($detailData)
Alternative variant without a blade directive:
{!! detail_view([
'model' => $model,
'title' => 'Detail table',
'rowFields' => [
'id',
'active',
'icon',
'created_at'
]
]) !!}
Setting custom options
Rows
Simple example:
@detailView([ 'model' => $model, 'rowFields' => [ [ 'label' => 'First Name', // Row label. 'attribute' => 'first_name', // Attribute, by which the row data will be taken from a model. ], [ 'label' => 'Last Name', 'value' => function ($model) { return $model->last_name; } ], ] ])
Arrays and objects
Since version 1.1.0 the model option can be an array or an object, not only an Eloquent model. For example, data from an API, a config or a DTO:
@detailView([ 'model' => [ 'name' => 'Apple', 'price' => 10, 'address' => ['city' => 'Minsk'], ], 'rowFields' => [ 'name', 'price', [ 'label' => 'City', 'attribute' => 'address.city', // A dot notation for nested arrays and objects ], [ 'label' => 'Price with tax', 'value' => function ($data) { // The callback gets the same array or object return $data['price'] * 2; }, ], ] ])
A missing key or property is shown as an empty value.
If you have your own row class (extended from BaseRow), it works with Eloquent models as before. To show arrays and objects with it, override the getDataValue($data) method.
Formatters
There are the next formatter keys:
- html - is for passing a row content with html tags. The value is not escaped, so use it only for trusted content.
- image - is for inserting a row data in to
srcattribute of<img>tag. The value is escaped. - text - the default formatter. It removes html tags with
strip_tags()and escapes special characters (&,<,>, quotes). IfallowableTagsoption is set, the allowed tags stay and the value is not escaped. - url - is for inserting a row data in to
hrefattribute of<a>tag. The value is escaped. Thetitleoption (a link text) is not escaped, so it can contain html, like an icon.
For that keys there are the next formatters:
HtmlFormatterImageFormatterTextFormatterUrlFormatter
Also you can set formatter with some addition options. See the next simple example:
@detailView([ 'model' => $model, 'rowFields' => [ [ 'attribute' => 'url', 'format' => [ 'class' => Itstructure\DetailView\Formatters\UrlFormatter::class, 'title' => 'Source', 'htmlAttributes' => [ 'target' => '_blank' ] ] ], [ 'attribute' => 'content', 'format' => 'html' ] ] ])
Table heads
To set column titles, you can set captionColumnConfig and valueColumnConfig as in example:
@detailView([ 'model' => $model, 'captionColumnConfig' => [ 'label' => 'Custom title column', 'htmlAttributes' => [ 'class' => 'th-title-class' ] ], 'valueColumnConfig' => [ 'label' => 'Custom value column', 'htmlAttributes' => [ 'class' => 'th-value-class' ] ], 'rowFields' => [ [ 'attribute' => 'content', ] ] ])
To hide all table row with head titles:
@detailView([ 'model' => $model, 'showHead' => false, 'rowFields' => [ [ 'attribute' => 'content', ] ] ])
Complex extended example
@php $detailData = [ 'model' => $model, 'title' => 'Detail title', // It can be empty '' 'htmlAttributes' => [ 'class' => 'table table-bordered table-striped' ], 'captionColumnConfig' => [ 'label' => 'Custom title column', 'htmlAttributes' => [ 'class' => 'th-title-class' ] ], 'valueColumnConfig' => [ 'label' => 'Custom value column', 'htmlAttributes' => [ 'class' => 'th-value-class' ] ], 'rowFields' => [ [ 'attribute' => 'id', // REQUIRED if value is not defined. Attribute name to get row model data. 'label' => 'ID', // Row label. 'htmlAttributes' => [ 'class' => 'tr-class' ] ], [ 'label' => 'Active', // Row label. 'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically. return '<span class="icon fas '.($model->active == 1 ? 'fa-check' : 'fa-times').'"></span>'; }, 'format' => 'html', // To render row content without lossless of html tags, set 'html' formatter. ], [ 'label' => 'Url link', // Row label. 'attribute' => 'url', // REQUIRED if value is not defined. Attribute name to get row model data. 'format' => [ // Set special formatter. $model->{$this->attribute} will be inserted in to 'href' attribute of <a> tag. 'class' => Itstructure\DetailView\Formatters\UrlFormatter::class, // REQUIRED. For this case it is necessary to set 'class'. 'title' => 'Source', // title between a tags. 'htmlAttributes' => [ // Html attributes for <a> tag. 'target' => '_blank' ] ] ], [ 'label' => 'Icon', // Row label. 'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically. return $model->icon; }, 'format' => [ // Set special formatter. If $model->icon value is a url to image, it will be inserted in to 'src' attribute of <img> tag. 'class' => Itstructure\DetailView\Formatters\ImageFormatter::class, // REQUIRED. For this case it is necessary to set 'class'. 'htmlAttributes' => [ // Html attributes for <img> tag. 'width' => '100' ] ] ], 'created_at', // Simple row setting by string. ] ]; @endphp
@detailView($detailData)
Note: the title option is not escaped, so it can contain html. Do not put a user input there without escaping.
Tests
The package has its own tests. They check rows, Eloquent models, arrays and objects, formatters, html attributes, the @detailView directive and the publish command.
Good to know before you start:
- The tests do not need a database server and do not need a
.envfile. They use SQLite in memory, everything is created and deleted automatically. - The tests work on all supported versions: PHP 7.1 ... 8.5 and Laravel 5.5 ... 13.
- When you install dependencies of the package, the
vendorfolder and thecomposer.lockfile appear in the package folder. It is normal. They are in.gitignore, so they will not get into git.
Below there are several ways to run the tests. Choose the one, which fits you.
Way 1. You have PHP and Composer on your computer
-
Get the package source code.
If you have git, run command:
git clone https://github.com/itstructure/laravel-detail-view.gitIf you do not have git, open https://github.com/itstructure/laravel-detail-view, press the green Code button, then Download ZIP, and unpack the archive.
-
Go to the package folder:
cd laravel-detail-view -
Install dependencies (it is enough to do it once):
composer installComposer itself chooses the Laravel and PHPUnit versions, which fit your PHP version.
Note! PHP must have
pdo_sqliteandmbstringextensions. Usually they are already enabled. If not, Composer or the tests will tell you about it. -
Run the tests:
composer testOr the same directly:
- Linux / macOS:
vendor/bin/phpunit - Windows:
vendor\bin\phpunit
- Linux / macOS:
-
If you want to run only one test, add
--filterwith the test name:vendor/bin/phpunit --filter test_array
If everything is fine, you will see a message like: OK (31 tests, 56 assertions).
Way 2. You have nothing except Docker
You do not need to install PHP and Composer. Docker downloads them by itself.
-
Get the package source code and go to the package folder, as in steps 1 and 2 of Way 1.
-
Install dependencies (it is enough to do it once).
For Linux, macOS and WSL terminal:
docker run --rm --user "$(id -u):$(id -g)" -e COMPOSER_HOME=/tmp/composer -v "$PWD":/app -w /app composer:2 composer update --no-interaction
For Windows PowerShell:
docker run --rm -v "${PWD}:/app" -w /app composer:2 composer update --no-interaction
Here:
--user "$(id -u):$(id -g)"- files in thevendorfolder will belong to you, not to therootuser. In Windows PowerShell it is not needed.-v "$PWD":/app- the current folder (package folder) is available inside the container as/app.
-
Run the tests.
For Linux, macOS and WSL terminal:
docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/app -w /app php:8.4-cli php vendor/bin/phpunit
For Windows PowerShell:
docker run --rm -v "${PWD}:/app" -w /app php:8.4-cli php vendor/bin/phpunit
To run only one test, add
--filter test_nameto the end of the command.
Note! In step 2 Composer installs the newest versions of Laravel and PHPUnit. They need a new PHP, so in step 3 use php:8.4-cli or newer. To check an old PHP or Laravel version, see Way 3.
Way 3. Check a specific PHP and Laravel version with Docker
It is useful, when you want to make sure that the package works for example on PHP 7.3 with Laravel 8.
-
Choose a pair of versions from the table:
PHP image Laravel Testbench php:7.1-cli5.5.*3.5.*php:7.2-cli5.8.*3.8.*php:7.2-cli6.*4.*php:7.3-cli7.*5.*php:7.3-cli8.*6.*php:8.0-cli9.*7.*php:8.1-cli10.*8.*php:8.2-cli11.*9.*php:8.3-cli12.*10.*php:8.3-cli13.*11.*Testbench (
orchestra/testbench) is a tool for testing Laravel packages. Each Laravel version needs its own Testbench version, that is why it is in the table. -
Go to the package folder. If you have already installed dependencies before, delete the
vendorfolder and thecomposer.lockfile. -
Install dependencies for the chosen pair. Example for PHP 7.3 + Laravel 8 (Linux, macOS, WSL terminal):
docker run --rm --user "$(id -u):$(id -g)" -e COMPOSER_HOME=/tmp/composer -v "$PWD":/app -w /app composer:2 sh -c "composer config platform.php 7.3.33 && composer config policy.advisories.block false && composer require --no-update 'laravel/framework:8.*' && composer require --dev --no-update 'orchestra/testbench:6.*' && composer update --no-interaction --no-plugins"
What this command does:
composer config platform.php 7.3.33- tells Composer to choose packages for PHP 7.3, because Composer itself works on a newer PHP inside its container.composer config policy.advisories.block false- allows installing old Laravel versions. New Composer refuses to install versions with known security advisories. It is OK for running tests, but never do it in a real project.composer require --no-update ...- sets the chosen Laravel and Testbench versions.--no-plugins- old Laravel versions bring Composer plugins, which are not needed for tests and are blocked by new Composer.
For another pair, change
7.3.33,8.*and6.*to the values from the table. Forplatform.phpyou can write any version of the chosen PHP, for example7.1.33,8.0.30,8.2.0. -
Run the tests with the PHP image from the table:
docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/app -w /app php:7.3-cli php vendor/bin/phpunit
-
Important! The command in step 3 changes the
composer.jsonfile of the package. When you finish, return it back and delete the installed files:git checkout composer.jsonAnd delete the
vendorfolder and thecomposer.lockfile.If you downloaded the package as ZIP (without git), just download and unpack it again.
Way 4. Automatic tests on GitHub
You do not need to do anything. After each push and each pull request, GitHub runs the tests by itself on all version pairs from the table above (and a few more).
Results are on the Actions tab of the repository: https://github.com/itstructure/laravel-detail-view/actions
Green check mark - all tests passed. Red cross - something is broken, open it to see the details.
The settings are in the .github/workflows/tests.yml file.
If the package is installed in your Laravel project
-
Your project tests and the package tests do not mix.
When you run tests of your project (
composer test,php artisan testorvendor/bin/phpunitin the project folder), only tests of your project are run. The projectphpunit.xmlfile points to the projecttestsfolder, and the package lies invendor/itstructure/laravel-detail-view. So the package tests are not run there. -
Usually there are no package tests in your project at all.
By default Composer downloads the package as an archive. The
testsfolder, the.githubfolder and thephpunit.xml.distfile are excluded from this archive (see the.gitattributesfile), because they are not needed for the package to work. So invendor/itstructure/laravel-detail-viewthere is only the package itself. -
Exception: installation with
--prefer-source.If the package is installed with
--prefer-source(Composer makesgit cloneinstead of downloading an archive), or from a local folder (see "If you are testing this package from a local server directory" above), then the whole repository is in the project, the tests too.It does not break anything: your project tests still do not run them.
-
How to run the package tests in this case.
Do not run them through your project, the project has no tools needed for the package tests.
Run them in the package folder itself, as described in Way 1 or Way 2:
- If the package is installed from a local folder (
"type": "path"), go to that local folder, for example../laravel-detail-view. - Otherwise, just clone the package repository to a separate folder.
- If the package is installed from a local folder (
License
Copyright © 2021-2026 Andrey Girnik girnikandrey@gmail.com.
Licensed under the MIT license. See LICENSE.txt for details.
