PHP code example of hcakir / laravel-11-media-manager

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

    

hcakir / laravel-11-media-manager example snippets



composer 


Hcakir\Laravel11MediaManager\Providers\MediaManagerServiceProvider::class,



php artisan vendor:publish --provider="Hcakir\\Laravel11MediaManager\\Providers\\MediaManagerServiceProvider"




php artisan migrate



php artisan storage:link



use Hcakir\Laravel11MediaManager\Traits\MediaTrait;

class YourModel extends Model
{
    use MediaTrait;
}



if ($request->hasFile('media')) {
    $productCategory->attachMedia($request->file('media'));
}



if ($request->hasFile('media')) {
    $productCategory->syncMedia($request->file('media'));
}



$productCategory->detachMedia();
$productCategory->delete();



public function deleteMedia(Request $request, $categoryId, $mediaId)
{
    $productCategory = ProductCategory::findOrFail($categoryId);
    $media = $productCategory->media()->findOrFail($mediaId);
    $mediaPaths = [$media->path];
    MediaHelper::deleteMedia($mediaPaths);
    return response()->json(['success' => true]);
}



public function edit(string $id)
{
$productCategory = ProductCategory::findOrFail($id);
$media = $productCategory->media()->get();
return view('backend.pages.product_category.edit', compact('productCategory', 'media'));
}


use Hcakir\Laravel11MediaManager\Helpers\MediaHelper;

$mediaFiles = request()->file('media');
$mediaIds = MediaHelper::storeMedia($mediaFiles);