Package for upload and store any media files.

1.1.0 2022-07-01 11:55 UTC

This package is auto-updated.

Last update: 2024-04-29 04:47:50 UTC


README

Latest Version on Packagist Total Downloads Build Status

Laravel-Mediable is a package for easily uploading and attaching media files to models with Laravel/Lumen.

Installation

Via Composer

$ composer require simonmarcellinden/mediable

Register the package's service provider in config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.

Open and add the service provider to bootstrap/app.php

	$app->register(\SimonMarcelLinden\Mediable\MediableServiceProvider::class);

Publish the configurations

Run this on the command line from the root of your project:

$ no config needed

Run the migrations to add the required tables to your database.

$ php artisan migrate

Example Usage

Example if you upload need a model for Images

Create a eloquent Model and Controller

    php artisan make:model Image -c

Extend your eloquent model with the model from this package.

<?php

namespace App\Models;

use SimonMarcelLinden\Mediable\Models\Media;

class Image extends Media {
	/**
	* If you want to use a different table than media , please specify it here.
	*
	* @var string
	*/
	protected $table = 'images';

	/**
	 * If you want your media to be stored in a specific file folder, then specify it here.
	 *
	 * @var array
	 */
	protected $basePath = 'images';
}

Extend your controller with the package controller and specify the eloquent model to be used in the controller.

<?php

namespace App\Http\Controllers;

use App\Models\Image;
use SimonMarcelLinden\Mediable\Http\Controllers\MediaController;

class ImageController extends MediaController {
	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $model = Image::class;
}

Configure the default routes

	$router->group(['prefix' => 'image'], function () use ($router) {
		$router->get('/{id}', 'ImageController@show');
		$router->post('/upload', 'ImageController@upload');
		$router->delete('/delete/{id}', 'ImageController@delete');
	});

If you need a many to many relation add the follow code to your media model.

use SimonMarcelLinden\Mediable\Models\Media;

class Image extends Media {
	/**
     * Get all of the user that are assigned this model.
     */
    public function users() {
        return $this->morphedByMany(Drink::class, 'mediable');
    }

	/**
     * Get all of the products that are assigned this model.
     */
    public function products() {
        return $this->morphedByMany(Product::class, 'mediable');
	}
}

And add the follow code to your Elequent model

class Product extends Model {
	use Uuids;

	public function media() {
        return $this->morphToMany(Image::class, 'mediable', 'mediables', 'mediable_id', "media_id");
    }
}

class User extends Model {
	use Uuids;

	public function media() {
        return $this->morphToMany(Image::class, 'mediable', 'mediables', 'mediable_id', "media_id");
    }
}

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email info@snerve.de instead of using the issue tracker.