Blog Detail

24

Dec
Store & retrieve content in Plain text files with Laravel cover image

arrow_back Store & retrieve content in Plain text files with Laravel

Sheets is a Laravel package to store, retrieve & index content stored as text files. Markdown & front matter are supported out of the box, but you can parse & extract data from your files in whatever format you prefer. Sheets can be added to any existing Laravel application and is a perfect fit for documentation sites & personal blogs.

Installation

You can install this package via composer:

composer require spatie/sheets

Laravel will auto-discover and register the SheetsServiceProvider, so no further setup is required.

After installing, you must publish the sheets.php configuration file:

php artisan vendor:publish --provider="Spatie\Sheets\SheetsServiceProvider" --tag="config"

Finally, you must create your first collection.

Usage

The Sheets instance is available through a facade, helper function, or dependency injection.

use Sheets;

Sheets::all();
sheets()->all();
use Spatie\Sheets\Sheets;

class SheetsController
{
    public function index(Sheets $sheets)
    {
        return view('sheets', [
            'sheets' => $sheets->all(),
        ]);
    }
}

Creating your first collection

A collection maps to a folder in your filesystem of choice. Sheets will look for a disk configured in config/filesystems.php with the same name as the collection—or you can configure the disk name yourself.

// config/filesystems.php
return [
    'disks' => [
        // ...
        'posts' => [
            'driver' => 'local',
            'root' => base_path('posts'),
        ],
    ],
];

// config/sheets.php
return [
    'collections' => ['posts'],
];

Sheets will create a repository for the posts folder in your application.

app/
config/
posts/
  hello-world.md
---
title: Hello, world!
---
# Hello, world!

Welcome to Sheets!

A repository has two public methods: all() and get($slug). You can get a repository instance through the collection method on Sheets.

Repository::all() will return an Illuminate\Support\Collection containing Spatie\Sheets\Sheet instances.

$repository = Sheets::collection('posts');

$repository->all();

Repository::get($slug) returns a single Sheet instance or null if nothing was found. A sheet’s slug field contains its filename without an extension.

Sheets::collection('posts')->get('hello-world');

A Sheet instance is very similar to an Eloquent model. It holds an array of attributes that are exposed as properties. By default, it will contain the path as a slug field, all front matter data, and a contents field containing an HTML representation of the contained Markdown.

$sheet = Sheets::collection('posts')->get('hello-world');

echo $sheet->slug;
// 'hello-world'

echo $sheet->title;
// 'Hello, world!'

echo $sheet->contents;
// '<h1>Hello, world!</h1><p>Welcome to Sheets!</p>'

You can create your own Sheet implementations with accessors just like Eloquent, but we’ll dive into that later.

Sheets are highly configurable. You can configure each collection separately. You can visit the complete detail of sheets configuration on Github

Published at : 24-12-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project