PHP code example of simonmarcellinden / mediable

1. Go to this page and download the library: Download simonmarcellinden/mediable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simonmarcellinden / mediable example snippets


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

    php artisan make:model Image -c



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';
}



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;
}

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

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');
	}
}

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");
    }
}