Blog Detail

14

Jun
Attach Media Files to Your Models with Laravel Media Model cover image

arrow_back Attach Media Files to Your Models with Laravel Media Model

When developing an app, you may want to attach media files to your models, such as images, videos, or documents. With this package, you can easily attach media files to your models. The media files are stored in the specified disk and are related to the model using your database. You can link local media files or remote ones without needing to download them to your server.

The main difference between this package and the other ones available is that this package allows you to relate media files to your models using a custom table per model. Allowing you to manage the media tables per model instead of one unique media table for all models.

Installation

You can install the package via composer:

composer require sertxudeveloper/laravel-media-model

Next, you should publish the config file and the migrations:

php artisan vendor:publish --provider "SertxuDeveloper\Media\MediaServiceProvider"

After that, you can run the new migrations:

php artisan migrate

Finally, you can start using the package!

Usage

You should modify the model that you want to attach media files to.

<?php

namespace App\Models;

use SertxuDeveloper\Media\HasMedia;
use SertxuDeveloper\Media\Interfaces\MediaInteraction;

class Message extends Model implements MediaInteraction
{
    use HasMedia;
}

As you can see, the model has been modified to use the HasMedia trait. Also, the model must implement the MediaInteraction interface.

Separated media tables

If you want to use a separated media table for each model, you should modify the model to that you want to attach media files.

<?php

namespace App\Models;

use SertxuDeveloper\Media\HasMedia;
use SertxuDeveloper\Media\Interfaces\MediaInteraction;

class Message extends Model implements MediaInteraction
{
    use HasMedia;

    public function getMediaTable(): string
    {
        return 'messages_media';
    }
}

The specified table needs to be created, you can create it manually or use one of the following commands.

php artisan media:create-table messages_media

or

php artisan media:create-table messages

or

php artisan media:create-table "App\Models\Message"

Attaching media files

Once you configured the model, you can attach media files to it. For example:

Note: If you don’t specify a disk, the default disk will be used.

<?php

$message = Message::find(1);

$message->addMediaFromDisk(path: '/images/image.jpg', disk: 'public');

You can also attach a remote file:

Note: This will not download the file to your server. It will only add the remote file path to the database.

<?php

$message = Message::find(1);

$message->addMediaFromUrl('https://www.sertxudeveloper.com/assets/logo.svg');

Also, you can attach a file content, this will save the file to the disk and attach it to the model.

Note: This is useful if you get the content of a file from an external source, like an email attachment read by a mail parser.

<?php

$message = Message::find(1);

$message->addMediaFromContent(
  content: file_get_contents('/tmp/tmpA3ds2'),
  originalName: 'image.jpg',
  toFolder: 'avatars',
  toDisk: 'public'
);

For more details, you can visit its complete documentation on Github.

Closing Notes

Are you on the quest for a trustworthy Custom Web Development Company? Hire Talented Developers from Codebrisk and leverage the full potential of the finest quality Custom Web Development Services. Codebrisk has great experience in developing CRM software, e-commerce, and SASS web apps for a variety of industries. Please Feel free to get in touch or launch a project with us.

Published at : 14-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project