Laravel package for creating Word documents with Eloquent ORM dependencies.

Overview

Laravel Eloquent Word

This package provides an elegant way to generate Word documents with Eloquent Models. Uses PHPOffice/PHPWord package to generate Word documents and Laravel Medialibrary to associate Word documents as eloquent model media.

CircleCI StyleCI

Medialibrary Compatibility

Version Medialibrary
v1.* ~ 7.20
v2.* ^ 8.0
v3.* ^ 9.0

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

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

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.

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

Downloading Word

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

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

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

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

License

Please see the license file for more information.

You might also like...
This Laravel package merges staudenmeir/eloquent-param-limit-fix and staudenmeir/laravel-adjacency-list to allow them being used in the same model.

This Laravel package merges staudenmeir/eloquent-param-limit-fix and staudenmeir/laravel-adjacency-list to allow them being used in the same model.

A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache.

Laravel OTP Introduction A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache. The ca

Laravel package to search through multiple Eloquent models.

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.

Package with small support traits and classes for the Laravel Eloquent models

Contains a set of traits for the eloquent model. In future can contain more set of classes/traits for the eloquent database.

Laravel package to create autonumber for Eloquent model

Laravel AutoNumber Laravel package to create autonumber for Eloquent model Installation You can install the package via composer: composer require gid

Laravel breeze is a PHP Laravel library that provides Authentication features such as Login page , Register, Reset Password and creating all Sessions Required.

About Laravel breeze To give you a head start building your new Laravel application, we are happy to offer authentication and application starter kits

This package gives Eloquent models the ability to manage their friendships.

Laravel 5 Friendships This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.

A small package for adding UUIDs to Eloquent models.

A small package for adding UUIDs to Eloquent models. Installation You can install the package via composer: composer require ryangjchandler/laravel-uu

This package provides a trait that will generate a unique uuid when saving any Eloquent model.

Generate slugs when saving Eloquent models This package provides a trait that will generate a unique uuid when saving any Eloquent model. $model = new

Releases(4.0)
Owner
Marvin Quezon
Developer.
Marvin Quezon
Add transparent versioning to Laravel 5's Eloquent ORM

Eloquent Versioned Adds transparent versioning support to Laravel 5.2's Eloquent ORM. WARNING: This repository is currently super-duper experimental.

Seb Barre 30 Jun 21, 2022
A Laravel package for parsing and processing Identity Documents

Laravel Identity Documents For general questions and suggestions join gitter: Package that allows you to handle documents like passports and other doc

365Werk 225 Nov 23, 2022
Livewire Notifier is a simple notifications system with zero dependencies above TALL-stack

Livewire Notifier is a simple notifications system with zero dependencies above TALL-stack (Tailwind CSS, Alpine.JS, Laravel and Livewire).

CodeSPB 18 Jul 27, 2022
Collection of agnostic PHP Functions and helpers with zero dependencies to use as foundation in packages and other project

Collection of agnostic PHP Functions and helpers This package provides a lot of very usefull agnostic helpers to use as foundation in packages and oth

padosoft 54 Sep 21, 2022
Use Laravel's built-in ORM classes to query cloud resources with Steampipe.

Laravel Steampipe Use Laravel's built-in ORM classes to query cloud resources with Steampipe, an open source CLI to instantly query cloud APIs using S

Renoki Co. 13 Nov 8, 2022
A Laravel 5.1 ORM example with Nerds as users.

laravel-nerds A Laravel 5.1 ORM example with Nerds as users. App Features Show all Nerds Add a Nerd Edit a Nerd Delete a Nerd Keeps Nerd's Name Keeps

Jeremy Kenedy 6 Aug 20, 2022
Learning Websocket by creating Custom Websocket-server package provided by Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling.

Chisty Md.Muzammel Hossain 3 Oct 25, 2022
This package should help you with creating and managing a Laravel DDD Application

This package should help you with creating and managing a Laravel DDD Application. This package is heavily inspired by "Laravel beyond CRUD" from Spatie.

J. Regner 158 Dec 25, 2022
A Laravel package for creating a Markdown driven blog in minutes.

Static Markdown Blog A Laravel package for generating static blog posts from Markdown files. Sometimes, you want a blog but don't necessarily want to

Danny 0 Nov 12, 2022
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022