wqa/nova-page-flexible-models

A Laravel Nova package to allow relating models as flexible content to pages

v1.4.0 2022-06-08 08:07 UTC

This package is auto-updated.

Last update: 2022-06-08 08:08:23 UTC


README

A package for Laravel Nova which extends Nova Page and Nova Flexible Content and enables you to relate Laravel models to your Nova Pages using Flexible Content.

Testimonials example

Installation

composer require wqa/nova-page-flexible-models

Usage

In your page template class you must include the HasFlexibleModels trait.

To add a flexible model field, use the addFlexibleModelField method in your fields definition.

You can use the getFlexibleModels method to get the models back from the page. It's useful to do this using a helper method as shown below to keep your blade files clean.

class AboutPage extends Template
{
    use WQA\NovaPageFlexibleModels\HasFlexibleModels;

    public function fields(Request $request)
    {
        return [
            Panel::make('Tesimonials', [
                $this->addFlexibleModelField('Testimonials', 'testimonials', Testimonial::class, 'author'),
            ])
        ];
    }

    public function testimonials(): Collection
    {
        return $this->getFlexibleModels('testimonials', Testimonial::class);
    }
}

To access the testimonials in a blade view you can call the helper method as above which will give you a collection of whichever model you have specified.

@foreach (Page::testimonials() as $testimonial)
    <blockquote>
        {{ $testimonial->body }}
        <cite>{{ $testimonial->author }}</cite>
    </blockquote>
@endforeach