PHP code example of yoelpc4 / laravel-media

1. Go to this page and download the library: Download yoelpc4/laravel-media 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/ */

    

yoelpc4 / laravel-media example snippets


class User extends \Illuminate\Database\Eloquent\Model implements \Yoelpc4\LaravelMedia\Contracts\Mediable {
    use \Yoelpc4\LaravelMedia\Concerns\InteractsWithMedia;
    
    /**
     * User morph one profile image
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function profileImage()
    {
        return $this->morphOne(\Yoelpc4\LaravelMedia\Models\Media::class, 'mediable')->whereGroup('profile image');
    }
}

/**
 * @inheritDoc
 */
public function registerMediaGroups()
{
    $this->addMediaGroup('contract document')
        ->setMimeTypes('application/pdf')
        ->setMaxFileLimit(3);
}

/**
 * @inheritDoc
 */
public function registerMediaGroups()
{
    $this->addMediaGroup('profile image')
        ->setMimeTypes(['image/jpeg', 'image/png'])
        ->setMaxFileLimit(1)
        ->registerMediaConversions(function () {
            $this->addMediaConversion('square-thumb')
                ->width(160)
                ->height(160);
        });
}

try {
$path = storage_path('avatar.jpg');

$media = User::first()->addFile($path)->toMediaGroup('profile image');
} catch (\Illuminate\Contracts\Filesystem\FileNotFoundException $e) {
    throw $e;
} catch (\Illuminate\Validation\ValidationException $e) {
    throw $e;
} catch (\Spatie\Image\Exceptions\InvalidImageDriver $e) {
    throw $e;
}

try {
$uploadedFile = request()->file('file');

$media = (new Project)->addFile($uploadedFile)->toMediaGroup('contract document');
} catch (\Illuminate\Contracts\Filesystem\FileNotFoundException $e) {
    throw $e;
} catch (\Illuminate\Validation\ValidationException $e) {
    throw $e;
} catch (\Spatie\Image\Exceptions\InvalidImageDriver $e) {
    throw $e;
}

$url = Media::first()->getMediaUrl();

$url = User::first()->profileImage->getMediaConversionUrl('square-thumb');
shell
php artisan vendor:publish --provider="Yoelpc4\LaravelMedia\MediaServiceProvider" --tag=config
shell
php artisan vendor:publish --provider="Yoelpc4\LaravelMedia\MediaServiceProvider" --tag=migrations
shell
php artisan vendor:publish --provider="Yoelpc4\LaravelMedia\MediaServiceProvider" --tag=resources