PHP code example of iamx / laravel-media

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

    

iamx / laravel-media example snippets




namespace App;

use iamx\Media\Traits\MediaTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use MediaTrait;
}



public function store(Request $request)
{
    $this->validate($request, [
        'avatar' => '

public function store(Request $request)
{
    $this->validate($request, [
        'avatar' => ' ->withThumbnail(200, 200) // Create a thumbnail of file, accepts with and height values
            ->withName('avatar') // Set custom media name
            ->toDisk('avatars') // Set storage disk
            ->toCollection('gallery') // Set collection name
            ->store();
}



$user = User::first();

// Verify is model has media
if($user->hasMedia()) {
    return response()->json($user->getMedia());
}

// Access to media
$user->getMedia();
// Or
$user->media_library;


public function update(Request $request, $id)
{
    $this->validate($request, [
        'avatar' => 'r'), $mediaId);
}

public function update(Request $request, $id)
{
    $this->validate($request, [
        'avatar' => 'ail = true;
    $thumbnailWidth = 200;
    $thumbnailHeight = 200;

    $user->updateMedia($request->file('avatar'), $mediaId, $mediaName, $createThumbnail, $thumbnailWidth, $thumbnailHeight);
    
}

public function destroy($id)
{
    $user = User::find($id);

    // Delete all related media
    $user->deleteMedia();

    // Delete a single media by id
    $mediaId = 3;
    $user->deleteMedia($mediaId);

    // Delete a entire collection
    $collectionName = "gallery";
    $user->deleteCollection($collectionName);
}