PHPackages                             corollarium/modelarium-medialibrary - 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. [API Development](/categories/api)
4. /
5. corollarium/modelarium-medialibrary

ActiveLibrary[API Development](/categories/api)

corollarium/modelarium-medialibrary
===================================

Integrates Spatie Laravel-media-library with Modelarium and GraphQL

v0.5.0(5y ago)015[1 issues](https://github.com/Corollarium/modelarium-medialibrary/issues)MITPHPPHP &gt;=7.2.0

Since Dec 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Corollarium/modelarium-medialibrary)[ Packagist](https://packagist.org/packages/corollarium/modelarium-medialibrary)[ Docs](https://github.com/Corollarium/modelarium-medialibrary/)[ RSS](/packages/corollarium-modelarium-medialibrary/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (9)Dependencies (7)Versions (10)Used By (0)

Modelarium - Laravel Media Library package
==========================================

[](#modelarium---laravel-media-library-package)

[![Latest Stable Version](https://camo.githubusercontent.com/b835c536510dbde703302266842323a2475c6711effa100bef3ed6770b8a9d47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f726f6c6c617269756d2f6d6f64656c617269756d2d6d656469616c6962726172792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/corollarium/modelarium-medialibrary)[![Total Downloads](https://camo.githubusercontent.com/99a066f66169c6f05c59d6ab8ef9251a64b57fbc3c3a9b6e9b4299bdbcbe0e25/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f726f6c6c617269756d2f6d6f64656c617269756d2d6d656469616c6962726172792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/corollarium/modelarium-medialibrary)[![License](https://camo.githubusercontent.com/4fefdd8f164272190abdfbcef4a3fd541edc14533f5934e9c4369315fcb79705/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f726f6c6c617269756d2f6d6f64656c617269756d2d6d656469616c6962726172792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/corollarium/modelarium-medialibrary)

---

This package integrates [Spatie's Laravel Media Library](https://github.com/spatie/laravel-medialibrary) to [Modelarium](https://github.com/Corollarium/modelarium), allowing GraphQL queries with [LighthousePHP](https://github.com/nuwave/lighthouse) to easily access media on models.

Installing
----------

[](#installing)

On your Laravel app run:

```
composer require corollarium/modelarium-medialibrary

```

Quick overview
--------------

[](#quick-overview)

This a Graphql file for a model with two different media collections, "image" and "map". Both have extra fields, "url" and "description". "Map" also has a conversion "thumb" with 150x150 (max, maintaining aspect ratio) thumbnails.

```
type Post
  @migrationSoftDeletes
  @migrationTimestamps {
  id: ID!

  name: String!
    @modelFillable
    @renderable(
        label: "Name"
        size: "large"
        itemtype: "name"
        card: true
        title: true
    )

  description: Text!
      @modelFillable
      @renderable(label: "Description", itemtype: "description")

  imageUrl: Url @migrationSkip

  image: LaravelMediaLibraryData
      @migrationSkip
      @laravelMediaLibraryData(
            collection: "image"
            fields: ["url", "description"]
      )

  map: LaravelMediaLibraryData
      @migrationSkip
      @laravelMediaLibraryData(
            collection: "map"
            fields: ["url", "description"]
            conversions: [
                { name: "thumb", width: 150, height: 150 }
            ]
      )
}
```

This will automatically generate roughly the following code on your model class:

```
/**
 * This file was automatically generated by Modelarium on 2020-12-13T18:28:11+00:00
 */
namespace App\Models;

abstract class BasePost extends \Illuminate\Database\Eloquent\Model implements \Spatie\MediaLibrary\HasMedia
{
    use \Spatie\MediaLibrary\InteractsWithMedia;

    // ...lots of base stuff...

    /**
     * Configures Laravel media-library
     */
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('image');

        $this->addMediaCollection('map');
    }

    /**
     * Returns a collection media from Laravel-MediaLibrary
     */
    public function getMediaImageCollection(): \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection
    {
        return $this->getMedia('image');
    }

    /**
     * Returns custom fields for the media
     */
    public function getMediaImageCustomFields(): array
    {
        return ['url', 'description'];
    }

    /**
     * Returns the media attribute (url) for the image
     */
    public function getImageUrlAttribute(): string
    {
        $image = $this->getMediaImageCollection()->first();
        if ($image) {
            return $image->getUrl();
        }
        return '';
    }

    /**
     * Returns media attribute for the image media with custom fields
     */
    public function getImageAttribute(): array
    {
        $image = $this->getMediaImageCollection()->first();
        if ($image) {
        $customFields = [];
        foreach ($this->getMediaImageCustomFields() as $c) {
            $customFields[$c] = $image->getCustomProperty($c);
        }
        return [
            'url' => $image->getUrl(),
            'fields' => json_encode($customFields)
        ];
        }
        return [];
    }

    /**
     * Configures Laravel media-library conversions
     */
    public function registerMediaConversions(?\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
    {
        $this->addMediaConversions('thumb')->width('150')->height('150');
    }

    /**
     * Returns a collection media from Laravel-MediaLibrary
     */
    public function getMediaMapCollection(): \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection
    {
        return $this->getMedia('map');
    }

    /**
     * Returns custom fields for the media
     */
    public function getMediaMapCustomFields(): array
    {
        return ['url', 'description'];
    }

    /**
     * Returns the media attribute (url) for the map
     */
    public function getMapUrlAttribute(): string
    {
        $image = $this->getMediaMapCollection()->first();
        if ($image) {
            return $image->getUrl();
        }
        return '';
    }

    /**
     * Returns media attribute for the map media with custom fields
     */
    public function getMapAttribute(): array
    {
        $image = $this->getMediaMapCollection()->first();
        if ($image) {
        $customFields = [];
        foreach ($this->getMediaMapCustomFields() as $c) {
            $customFields[$c] = $image->getCustomProperty($c);
        }
        return [
            'url' => $image->getUrl(),
            'fields' => json_encode($customFields)
        ];
        }
        return [];
    }

    // ...more stuff here...
}
```

Documentation
-------------

[](#documentation)

The `@laravelMediaLibraryData` directive supports the following arguments:

TODO

FAQ
---

[](#faq)

### I need some custom conversion on my collection.

[](#i-need-some-custom-conversion-on-my-collection)

If the basic arguments on the directive are not enough, just override the method on your model class:

```
class Post extends BasePost
{
    public function registerMediaConversions(?\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
    {
        // do your stuff here
    }
}
```

### How do I get only the image url for a single image in my type?

[](#how-do-i-get-only-the-image-url-for-a-single-image-in-my-type)

Define a `xxxUrl` on your type, where `xxx` is your collection name. which is filled automatically by the attribute method on the model. Don't forget to add `@migrationSkip` so it won't be created in the migration. Example:

```
type Post {
    # ...

    # this field is filled automatically by the model
    imageUrl: Url @migrationSkip

    image: LaravelMediaLibraryData
        @migrationSkip
        @laravelMediaLibraryData(
            collection: "image"
            singleFile: true
            fields: ["url", "description"]
        )
}
```

your query will look like this:

```
query($id: ID!) {
    post(id: $id) {
        id

        # ... other fields

        imageUrl # this will return the image url
    }
}
```

### How do I get the full image data in a query?

[](#how-do-i-get-the-full-image-data-in-a-query)

Just define the `LaravelMediaLibraryData` field in your type. Example:

```
type Post {
    image: [LaravelMediaLibraryData!]
        @migrationSkip
        @laravelMediaLibraryData(
            collection: "image"
            fields: ["url", "description"]
        )
}
```

On your query, fetch the field

```
query($id: ID!) {
    post(id: $id) {
        id

        # ... other fields

        image {
            url # the url, a String
            fields # a JSON-encoded string with your custom fields if they exist
        }
    }
}
```

You can easily access only the first one too:

```
type Post {
    image: [LaravelMediaLibraryData!]
        @migrationSkip
        @laravelMediaLibraryData(
            collection: "image"
            fields: ["url", "description"]
        )

    # add this to get the first one:
    imageFirst: LaravelMediaLibraryData @migrationSkip
}
```

In your query:

```
query($id: ID!) {
    post(id: $id) {
        id

        # ... other fields

        imageFirst {
            url # the url, a String
            fields # a JSON-encoded string with your custom fields if they exist
        }
    }
}
```

I want a single image in the collection, never more than one. How do I do that?
-------------------------------------------------------------------------------

[](#i-want-a-single-image-in-the-collection-never-more-than-one-how-do-i-do-that)

Declare a `singleFile` collection. Notice that the return type here is a single `LaravelMediaLibraryData`, not an array:

```
type Post {
    image: LaravelMediaLibraryData
        @migrationSkip
        @laravelMediaLibraryData(
            collection: "image"
            singleFile: true
            fields: ["url", "description"]
        )

    # the [collection]First attribute is also available, so you can mix it with non-singleFile collections:
    imageFirst: LaravelMediaLibraryData @migrationSkip

}
```

Then access it normally:

```
query($id: ID!) {
    post(id: $id) {
        id

        # ... other fields

        image {
            url # the url, a String
            fields # a JSON-encoded string with your custom fields if they exist
        }
    }
}
```

### How do I add thumbnails?

[](#how-do-i-add-thumbnails)

Add a `conversions` field. Two accessor methods will be created on your model for each collection: `[CollectionName][ConversionName]HTML` (which generates the full HTML tag) and `[CollectionName][ConversionName]Url` (just the URL). Add them to your GraphQL type (and remember to `@migrationSkip` them) to access them. Example:

```
type Post {
    image: LaravelMediaLibraryData
        @migrationSkip
        @eagerLoad(name: "media")
        @laravelMediaLibraryData(
            collection: "image"
            conversions: [{ name: "thumb", width: 128, height: 128 }]
        )

    imageThumbHTML: String @migrationSkip @renderable(show: true)

    imageThumbUrl: Url @migrationSkip @renderable(show: true)

}
```

Easy to fetch:

```
query($id: ID!) {
    post(id: $id) {
        id

        # ... other fields

        imageThumbUrl
    }
}
```

### How do I get the responsive image data in a query?

[](#how-do-i-get-the-responsive-image-data-in-a-query)

Add a `conversions` field with `responsive: true`.

```
type Post {
    image: LaravelMediaLibraryData
        @migrationSkip
        @eagerLoad(name: "media")
        @laravelMediaLibraryData(
            collection: "image"
            conversions: [{ name: "thumb",  responsive: true}]
        )
}
```

TODO: explain how to get data

### How do I eager load?

[](#how-do-i-eager-load)

Add an `@eagerLoad` directive to eager load the table and avoid a N+1 problem.

```
type Post {
    image: LaravelMediaLibraryData
        @migrationSkip
        @eagerLoad(name: "media")
        @laravelMediaLibraryData(
            collection: "image"
            fields: ["url", "description"]
        )
}
```

Sponsors
--------

[](#sponsors)

[![Corollarium](https://camo.githubusercontent.com/4e7626b7c7d8d11b8e225bb74e005a48d99e663b4163c56f262f65d60cf23cf7/68747470733a2f2f636f726f6c6c617269756d2e6769746875622e636f6d2f6d6f64656c617269756d2f6c6f676f2d686f72697a6f6e74616c2d34303070782e706e67)](https://corollarium.com)

We want to thanks to [Spatie](https://github.com/spatie) for its wonderful [Spatie's Laravel Media Library](https://github.com/spatie/laravel-medialibrary).

Contributing [![contributions welcome](https://camo.githubusercontent.com/9e93e892d0685e1bf7a1d0bd7c8410d6ecf2086a0a7b48dd58a6b96fa556ea2a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e747269627574696f6e732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c6174)](https://github.com/Corollarium/modelarium-medialibrary/issues)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#contributing-)

Any contributions are welcome. Please send a PR.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 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 ~5 days

Recently: every ~11 days

Total

9

Last Release

1932d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd08fdd8d94a326cf2a50e349ec3f4b377d166cffbd5063ce7015eccc843f454?d=identicon)[brunobg](/maintainers/brunobg)

---

Top Contributors

[![brunobg](https://avatars.githubusercontent.com/u/798800?v=4)](https://github.com/brunobg "brunobg (31 commits)")

---

Tags

weblaravelgraphqlmediamodelarium

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/corollarium-modelarium-medialibrary/health.svg)

```
[![Health](https://phpackages.com/badges/corollarium-modelarium-medialibrary/health.svg)](https://phpackages.com/packages/corollarium-modelarium-medialibrary)
```

###  Alternatives

[nuwave/lighthouse

A framework for serving GraphQL from Laravel

3.5k10.7M93](/packages/nuwave-lighthouse)[mll-lab/laravel-graphiql

Easily integrate GraphiQL into your Laravel project

683.2M9](/packages/mll-lab-laravel-graphiql)[scrnhq/laravel-bakery

An on-the-fly GraphQL Schema generator from Eloquent models for Laravel.

10518.2k](/packages/scrnhq-laravel-bakery)[yakovenko/laravel-lighthouse-graphql-multi-schema

A Laravel package that provides multi-schema support for Lighthouse GraphQL.

1562.5k](/packages/yakovenko-laravel-lighthouse-graphql-multi-schema)[thecodingmachine/graphqlite-laravel

A Laravel service provider package to help you get started with GraphQLite in Laravel.

1852.3k1](/packages/thecodingmachine-graphqlite-laravel)

PHPackages © 2026

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