1. Go to this page and download the library: Download brysem/eloquent-files 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/ */
brysem / eloquent-files example snippets
Bryse\Eloquent\Files\FilesServiceProvider::class,
use Bryse\Eloquent\Files\Traits\HasFiles;
class User extends Authenticatable
{
use HasFiles;
}
// Returns an array of the files that have been uploaded.
// The second parameter is the path inside your storage_path().
$user->upload(request()->file(), 'users');
// Get a collection (array) of files that belong to the model.
$files = $user->files;
// Get a collection of image files that belong to the model.
$images = $user->files()->images()->get();
// Get a collection of video files that belong to the model.
$videos = $user->files()->videos()->get();
// You can go crazy and grab all text files created three days ago.
$files = $user->files()->where('created_at', '<=', Carbon\Carbon::now()->subDays(3))->where('type', 'like', 'text/%')->get();
// UserController.php
public function uploadProfileImage(Request $request, User $user)
{
// Remove all previous images.
$user->files->each(function($file) {
$file->delete();
});
$user->upload(request()->file(), 'users');
}
// User.php
public function getImageAttribute()
{
return $this->files()->images()->first()->url;
}
// Usage
<img src="{{ $user->image }}">