PHPackages                             emunozhern/laravel-toc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. emunozhern/laravel-toc

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

emunozhern/laravel-toc
======================

Table of contents generator Laravel package for posts, news, articles or any other text

014PHP

Since Dec 7Pushed 3y agoCompare

[ Source](https://github.com/emunozhern/laravel-toc)[ Packagist](https://packagist.org/packages/emunozhern/laravel-toc)[ RSS](/packages/emunozhern-laravel-toc/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

"Table of contents" generator Laravel package
=============================================

[](#table-of-contents-generator-laravel-package)

This package provides clickable Table Of Contents for your posts, news, articles etc based on h2-h6 html tags from input text.

[![Build Status](https://camo.githubusercontent.com/c3de9d04d5455270c6473cbaaafcd31d4584d4990080647aee4281cd036fc7fc/68747470733a2f2f7472617669732d63692e6f72672f4e696b6f6c61794f736b696e2f636f6e74656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/NikolayOskin/contents)[![laravel version](https://camo.githubusercontent.com/173354e5a808efb589d11fac6ad6dc42e04194e2605b7fc5ea0490601dac1847/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c25323076657273696f6e2d254532253839254137352e352d7265642e737667)](https://github.com/NikolayOskin/contents)

Usage
-----

[](#usage)

Let's say you have some posts (from WYSIWYG editor or something) stored in your database which looks like this:

```
First h2 header
Some text
First h3 header
Some text
2nd h3 header
Some text
First h4 header
Some text
2nd h4
Some text
Latest header

```

And you have some PostController with show() method which sends your Post model object data to your view.

```
public function show ($id)
{
    $post = Post::find($id);
    $header = $post->title;
    $body = $post->body;
    return view('post', compact('header', 'body');
}
```

Your 'post.blade.php' view looks stupid simple like this:

```

    {{$header}}
    {!! $body !!}

```

This is how you can use this package to generate "Table of contents" in controller:

```
public function show ($id, Contents $contents)
{
    $post = Post::find($id);
    $header = $post->title;
    $body = $contents->fromText($post->body)->getHandledText();
    $contents = $contents->getContents();
    return view('post', compact('header', 'body', 'contents');
}
```

Now you passed "contents" array where your header tags are stored. Also, as you can see you passed "handledText", which has id attributes inside header html tags.

Your view:

```

    {{$header}}
    @if(!empty($contents))
        @include('nikolay-oskin.contents.table', $contents)
    @endif
    {!! $post !!}

```

table.blade.php is the view with ul-li tags where you can add your css styles or make any other customization.

#### Result

[](#result)

[![Alt text](https://camo.githubusercontent.com/98359bb9ae559983505d17db0e4fde95680d397c6ff2f12a8e3a0032edd9530f/687474703a2f2f6a6f78692e72752f676d764259614e48715a3070646d2e706e67 "Optional title")](https://camo.githubusercontent.com/98359bb9ae559983505d17db0e4fde95680d397c6ff2f12a8e3a0032edd9530f/687474703a2f2f6a6f78692e72752f676d764259614e48715a3070646d2e706e67)

Options
-------

[](#options)

By default contents created for h2-h6 tags. You can set headers tags which have been included in 'Table of contents'.

Example:

```
public function show ($id, Contents $contents)
{
    $post = Post::find($id);
    $contents->fromText($post->body)->setTags(['h2', 'h3']);
    $body = $contents->getHandledText();
    $contents = $contents->getContents();
    return view('post', compact('body', 'contents');

    // Only h2 and h3 headers will be added to table of contents
}
```

You can also set text min-length number so if the text length is below this number then $contents array will be empty and $contents-&gt;getHandledText() returns original text.

Example:

```
public function show ($id, Contents $contents)
{
    $post = Post::find($id);
    $contents->fromText($post->body)->setTags(['h2', 'h3'])->setMinLength(5000);
    $body = $contents->getHandledText();
    $contents = $contents->getContents();
    return view('post', compact('body', 'contents');

    // Table of contents will not be shown for texts length less than 5000 chars.
}
```

Install
-------

[](#install)

For Laravel 5.5+

```
composer require nikolay-oskin/contents
```

To copy views to your resource/views directory:

```
php artisan vendor:publish --provider="NikolayOskin\Contents\ContentsServiceProvider"
```

License
-------

[](#license)

MIT

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity24

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8387d3bf79ff2e02bc19ea4f940b381ad44ce7e449e19f82404afbb7941be0e7?d=identicon)[emunozhern](/maintainers/emunozhern)

---

Top Contributors

[![NikolayOskin](https://avatars.githubusercontent.com/u/39841953?v=4)](https://github.com/NikolayOskin "NikolayOskin (40 commits)")

### Embed Badge

![Health badge](/badges/emunozhern-laravel-toc/health.svg)

```
[![Health](https://phpackages.com/badges/emunozhern-laravel-toc/health.svg)](https://phpackages.com/packages/emunozhern-laravel-toc)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
