[![SensioLabsInsight][ico-sensio]][link-sension]
Rivet package provides foundation for easy and rapid development of library-like, attachable and sortable Eloquent models for your app. Models representing file attachments, images or similar can be easily added to your app and attached to existing models.
Via Composer
$ composer require luminark/rivet
Eloquent models that have one or many rivet models must use the HasRivetsTrait
trait. This trait takes advantage of PHP's magic methods to allow quick use without much coding, while leaving room for further optimization.
For example, lets assume we have a model Page
and we want it to have a collection of Attachment
s and a single Image
.
class Page extends Model
{
use HasRivetsTrait;
protected function getRivetsConfig()
{
return [
'attachments' => ['collection', Attachment::class],
'image' => ['property', Image::class]
];
}
}
We use the getRivetsConfig
method to define attributes on the parent model which map to a collection or a property of attachable models. Attachable models inherit from the base Luminark\Attachment\Models\Rivet
class.
class Image extends Rivet
{
protected $fillable = ['title', 'size'];
public static function getMorphToManyName()
{
return 'imageable';
}
}
Attaching classes can share the base rivets
table for quicker prototyping. Although this doesn't translate to a normalized database, it's a variant of the single table inheritance object mapping that allows us to develop and setup a working app very quickly. If you want the extending model class to share the base table, simply have it use the Luminark\Attachment\Traits\UsesRivetsTableTrait
trait.
If the extending model class is using its own database table, make sure to override the getMorphToManyName
method which is used to properly map attaching models to parent models via polymorphic many-to-many relationship.
The Attachment
model from our example can share the base table:
class Attachment extends Rivet
{
use UsesRivetsTableTrait;
protected function getSerializableAttributes()
{
return ['file', 'title'];
}
}
The base Rivet
class implements the Luminark\SerializableValues\Traits\HasSerializableValuesTrait
which gives it (and all extending classes) access to values
attribute. The contents of this attribute are serialized when being stored to the database, and deserialized when reading from it, which makes for a convenient storage of variable amount of data. To take advantage of this, you can use the getSerializableAttributes
method on the attaching model to define which elements of the values
array can be accessed as model's attributes. If the extending class is not sharing the rivets
table, a values
column should be added to extending class' table if this functionality is needed.
With the class from example above, the following would work as expected.
$page = Page::find(1);
$image = $page->image;
$image->title; // returns value of $image->values['title']
Models implementing the HasRivetsTrait
trait have several methods for setting and removing attachable models at disposal out of the box.
$page = Page::find(1);
// setModelName and unsetModelName for model as property
$page->setImage($model);
$page->unsetImage($modelIdOrModelObject);
// addModelName or removeModelName for models in collection
$page->addAttachment($model);
$page->removeAttachment($modelIdOrModelObject);
// Getters
$page->image;
$page->attachments;
A convenient class for processing files related to attachable models comes with the package. It is acessible via Luminark\Rivet\Facades\FileProcessor
facade or via dependency injection by type hinting Luminark\Rivet\Interfaces\FileProcessorInterface
. It has a single method processFile
which takes the attachable model object for which the file is being processed and reference to a file (a string
path, Symfony\Component\HttpFoundation\File\File
object or Symfony\Component\HttpFoundation\File\UploadedFile
object). This method will attempt to store the file to a storage disk defined in Laravel config and fire events.
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email mvrkljan@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.