-
composer.json
Open in GitHub{ // "require": { "php": "^7.4", // "spatie/laravel-medialibrary": "8.10.1" }, // }
-
app/Models/MediaLibrary.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; class MediaLibrary extends Model implements HasMedia { use InteractsWithMedia; public function registerMediaConversions(Media $media = null): void { $this->addMediaConversion('thumb') ->width(350) ->height(250); } }
-
resources/views/admin/media/create.blade.php
Open in GitHub@extends('admin.layouts.app') @section('content') <h1>@lang('media.create')</h1> {!! Form::open(['route' => ['admin.media.store'], 'method' =>'POST', 'files' => true]) !!} <div class="form-group"> {!! Form::label('image', __('media.attributes.image')) !!} {!! Form::file('image', ['accept' => 'image/*', 'class' => 'form-control' . ($errors->has('image') ? ' is-invalid' : ''), 'required']) !!} @error('image') <span class="invalid-feedback">{{ $message }}</span> @enderror </div> // @endsection
-
app/Http/Controllers/Admin/MediaLibraryController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Http\Requests\Admin\MediaLibraryRequest; use App\Models\MediaLibrary; class MediaLibraryController extends Controller { // public function store(MediaLibraryRequest $request): RedirectResponse { $image = $request->file('image'); $name = $image->getClientOriginalName(); if ($request->filled('name')) { $name = $request->input('name'); } MediaLibrary::first() ->addMedia($image) ->usingName($name) ->toMediaCollection(); return redirect()->route('admin.media.index')->withSuccess(__('media.created')); } // }