PHPackages                             ambengers/eloquent-word - 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. [Database &amp; ORM](/categories/database)
4. /
5. ambengers/eloquent-word

ActiveProject[Database &amp; ORM](/categories/database)

ambengers/eloquent-word
=======================

Laravel package for creating Word documents with Eloquent ORM dependencies.

5.0(2y ago)9452MITPHPPHP ^8.0

Since Oct 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ambengers/eloquent-word)[ Packagist](https://packagist.org/packages/ambengers/eloquent-word)[ Docs](https://github.com/ambengers/eloquent-word)[ RSS](/packages/ambengers-eloquent-word/feed)WikiDiscussions master Synced yesterday

READMEChangelog (7)Dependencies (5)Versions (14)Used By (0)

Laravel Eloquent Word
=====================

[](#laravel-eloquent-word)

This package provides an elegant way to generate Word documents with Eloquent Models. Uses [PHPOffice/PHPWord](https://github.com/PHPOffice/PHPWord) package to generate Word documents and [Laravel Medialibrary](https://github.com/spatie/laravel-medialibrary) to associate Word documents as eloquent model media.

[![CircleCI](https://camo.githubusercontent.com/23b879c48166e78956b03f0ba40e055c24c3e598a88f2d980ff67891926a7ce3/68747470733a2f2f636972636c6563692e636f6d2f67682f616d62656e676572732f656c6f7175656e742d776f72642f747265652f6d61737465722e7376673f7374796c653d737667)](https://circleci.com/gh/ambengers/eloquent-word/tree/master)[![StyleCI](https://camo.githubusercontent.com/be892eebfb668acb89849a91a6ac71aea3cb3e8ac01299ba141a9bca4d9adf7d/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3431393536303239392f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/419560299?branch=master)

Spatie Media Library Version Compatibility
------------------------------------------

[](#spatie-media-library-version-compatibility)

VersionMedialibraryv1.\*~ 7.20v2.\*^ 8.0v3.\*^ 9.0v4.\*^ 10.0v5.\*^ 10.0Installation
------------

[](#installation)

Via Composer

```
$ composer require ambengers/eloquent-word
```

Optionally, you can publish the config file by running the following command.

```
php artisan vendor:publish --tag=eloquent-word-config
```

Usage
-----

[](#usage)

### Eloquent Word class

[](#eloquent-word-class)

You can generate your Eloquent Word class using the command

```
$ php artisan make:eloquent-word PostWord
```

Optionally, you can pass a `--view` option to also generate a view template file.

```
$ php artisan make:eloquent-word PostWord --view=posts.word
```

By default, the class will be located at `App\Word` namespace. You can customize this in the config file.

Your Eloquent Word class will contain 2 methods:

- `getData()` provides the data to be used on the view
- `getView()` the name of the view file as word template

```
namespace App\Word;

use Ambengers\EloquentWord\AbstractEloquentWord;

class PostWord extends AbstractEloquentWord
{
    public function getData() : array
    {
        return [
            'title' => $this->model->title,
            'body'  => $this->model->body,
        ];
    }

    public function getView() : string
    {
        return 'posts.word';
    }
}
```

### View Template

[](#view-template)

Unlike PDF templates that uses html, Word templates are created using php scripts. So in your view template file, you can utilize the `@php` blade tags like so...

```
@php
// You automatically have access to $word within your view template,
// which is an instance of \PhpOffice\PhpWord\PhpWord::class...
$section = $word->addSection();

$section->addTitle($title);

$section->addTextBreak();

$section->addText($body);
@endphp
```

Within your view template, you automatically have access to `$word` variable which will give you an instance of `PhpOffice\PhpWord\PhpWord` class. This will allow you to get started formatting your Word document.

You can learn more by visiting the [PHPWord official documentation](https://phpword.readthedocs.io/en/latest/).

You can now use the Eloquent Word class from your controller (or anywhere in your application).

### Downloading Word

[](#downloading-word)

```
return app(PostWord::class)
    ->model($post)
    ->handle();
```

### Eloquent Word with Medialibrary

[](#eloquent-word-with-medialibrary)

This package also offers an elegant way to associate Word document to the Eloquent Model using Medialibrary package. To do that, you will need to use a trait on your Eloquent Word class.

```
use Ambengers\EloquentWord\InteractsWithMediaLibrary;

class PostWord extends AbstractEloquentWord
{
    use InteractsWithMediaLibrary;
}
```

Then on your controller, much like how you would do with medialibrary, just provide the collection name in which the Word document will be associated with.

```
return app(PostWord::class)
    ->model($post)
    ->toMediaCollection('reports')
    ->handle();
```

For additional convenience you can also chain other medialibrary methods.

```
return app(PostWord::class)
    ->model($post)
    ->toMediaCollection('reports')
    ->withCustomProperties(['foo' => 'bar'])
    ->withAttributes(['creator_id' => auth()->id()])
    ->handle();
```

Behind the scenes, Eloquent Word will forward these method calls to the medialibrary `FileAdder::class` so you can further take advantage of its features.

### Customizations

[](#customizations)

If you need to customize the default filename or extension, you can chain some setter methods when you call your Eloquent Word class.

```
return app(PostWord::class)
    ->model($post)
    ->filename('some-cool-filename')
    ->extension('odt')
    ->toMediaCollection('reports')
    ->handle();
```

Security
--------

[](#security)

If you discover any security related issues, please send the author an email instead of using the issue tracker.

License
-------

[](#license)

Please see the [license file](license.md) for more information.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~67 days

Recently: every ~183 days

Total

12

Last Release

926d ago

Major Versions

1.2 → v2.x-dev2021-10-27

2.0 → v3.x-dev2021-10-27

3.0 → v4.x-dev2022-04-08

4.0 → v5.x-dev2023-10-27

PHP version history (2 changes)1.0PHP ^7.4|^8.0

v4.x-devPHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/545ea2630b623d1556d595190fc55b0a32731291629f2378c632bcf0dd3164c2?d=identicon)[ambengers](/maintainers/ambengers)

---

Top Contributors

[![ambengers](https://avatars.githubusercontent.com/u/17848299?v=4)](https://github.com/ambengers "ambengers (58 commits)")

---

Tags

laraveleloquentword

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ambengers-eloquent-word/health.svg)

```
[![Health](https://phpackages.com/badges/ambengers-eloquent-word/health.svg)](https://phpackages.com/packages/ambengers-eloquent-word)
```

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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