Skip to content

Trait to simplify uploading images in Laravel

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

pfrug/file-upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

file-upload

Trait to simplify uploading images in Laravel

Installation

composer require pfrug/file-upload 1.0
// config/app.php
'providers' => [
    ...
    Pfrug\FileUpload\FileUploadServiceProvider::class,
];

Usage

Implement HasImageUploads in you model
Specify the fields that are of type file

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Pfrug\FileUpload\Traits\HasImageUploads;

class Post extends Model
{
    use HasImageUploads;

    protected $uploadableImages = [
        'image',
        'thumb'
    ];

In the controller it is not necessary to specify anything.
Only using the fill or create method the image will be saved correctly

public function store(Request $request)
{    
    // ... you code here, try-catch validate etc..
    Post::create($request->all());
    ...


public function update(Request $request, Post $post)
    // ... you code here, try-catch, validate data, etc..
    $post->fill($request->all());
    $post->save();

Or use direct assign

$post->title = $request->title ;
$post->image = $request->file('image');
$post->save();

Views

If you wish you can use the default view

    @include('fileupload::imageField', [
            'item' => $post,
            'field' => 'image',
            'cant_delete' => true
        ])

Configuration

php artisan vendor:publish --tag="fileupload-assets" --provider="Pfrug\FileUpload\FileUploadServiceProvider"
php artisan vendor:publish --tag="fileupload-lang" --provider="Pfrug\FileUpload\FileUploadServiceProvider"
php artisan vendor:publish --tag="fileupload-views" --provider="Pfrug\FileUpload\FileUploadServiceProvider"

This commands create files to:

"fileupload-assets"
{project}resources/css/vendor/fileupload/file-upload.css
{project}resources/js/vendor/fileupload/file-upload.js
{project}resources/img/vendor/fileupload/default.png

"fileupload-lang"
{project}resources/lang/{en-es}/fileupload.php

"fileupload-views"
{project}resources/view/vendor/fileupload/imageField.blade.php

Image storage

The images will be saved inside the folder storage/app/public/{model}
It is important to create a symbolic link, this can be done using the command php artisan storage:link

The path of the file will be stored in the database, so it is recommended that it be a field of type varchar(100)
ex: posts/9c744648c08730cd850c4d7dad9b998d.jpg

License

The MIT License (MIT). Please see License File for more information.

About

Trait to simplify uploading images in Laravel

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published