Skip to content

Commit 17ab035

Browse files
committed
initial commit
0 parents  commit 17ab035

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Laravel File Upload Helper package
2+
It will save you time.

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "plusemon/uploader",
3+
"description": "Laravel file upload helper package.",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Plusemon\\Uploader\\": "src/"
9+
}
10+
},
11+
"extra": {
12+
"laravel": {
13+
"providers": [
14+
"Plusemon\\Uploader\\UploaderServiceProvider"
15+
]
16+
}
17+
},
18+
"authors": [
19+
{
20+
"name": "emon khan",
21+
"email": "[email protected]"
22+
}
23+
],
24+
"minimum-stability": "dev",
25+
"require": {}
26+
}

src/UploaderServiceProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Plusemon\Uploader;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class UploaderServiceProvider extends ServiceProvider
8+
{
9+
10+
public function register()
11+
{
12+
//
13+
}
14+
15+
public function boot()
16+
{
17+
//
18+
}
19+
}

src/traits/HasUploader.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Plusemon\Uploader\traits;
4+
5+
use Illuminate\Support\Str;
6+
7+
/**
8+
* Name : Easy File Upload and View Helpers
9+
* Author : Emon Khan
10+
* Date : 12/05/2022
11+
*/
12+
trait HasUploader
13+
{
14+
/**
15+
* Generate the url for specific file related to the model
16+
*
17+
* @param string $property_name
18+
* @param string $type
19+
* @return string
20+
*
21+
*/
22+
public function urlOf($property_name)
23+
{
24+
if ($this->$property_name and file_exists(public_path($this->$property_name))) {
25+
return asset($this->$property_name);
26+
}
27+
}
28+
29+
/**
30+
* Upload under a specific model
31+
*
32+
* @param string $request_input_field_name
33+
* @param string $type
34+
* @return \App\Models\User
35+
*
36+
*/
37+
38+
39+
public function uploadFromRequest($request_input_field_name, $file_type = 'image')
40+
{
41+
if (request()->hasFile($request_input_field_name)) {
42+
$file = request()->file($request_input_field_name);
43+
$module_name = $module ?? $this->getTable();
44+
$unique_id = $this->id ?? uniqid();
45+
46+
$file_name = "{$module_name}-{$unique_id}-{$request_input_field_name}.{$file->extension()}";
47+
$dir = "uploads/{$module_name}/{$file_type}";
48+
49+
$saved = $file->move(public_path($dir), $file_name);
50+
$file_path = $dir . $file_name;
51+
$this->$request_input_field_name = $saved ? $file_path : null;
52+
}
53+
return $this;
54+
}
55+
}

0 commit comments

Comments
 (0)