https://github.com/dillingham/interacts-with-uploads
Adds upload method to requests
https://github.com/dillingham/interacts-with-uploads
Last synced: 4 months ago
JSON representation
Adds upload method to requests
- Host: GitHub
- URL: https://github.com/dillingham/interacts-with-uploads
- Owner: dillingham
- License: mit
- Created: 2021-04-15T21:58:29.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-15T21:59:44.000Z (about 5 years ago)
- Last Synced: 2025-07-31T21:03:08.065Z (11 months ago)
- Language: HTML
- Size: 185 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## InteractsWithUploads
Laravel trait adds FormRequest upload
## Install
```
composer require dillingham/interacts-with-uploads
```
And add the following trait to your FormRequest classes
```php
use \Dillingham\InteractsWithUploads;
```
Now you can call the following methods in controllers:
---
### New uploads
```
$request->upload('key')
```
```php
public function store(CreateProfileRequest $request)
{
$request->upload('banner');
$profile = Profile::create($request->validated());
return redirect()->route('profiles.show', $profile);
}
```
Store the file and add it's path to `validated()` for the same key: `banner`
This makes `$profile->banner` equal to `/uploads/asfos8asfoafaf9q3wf.jpg`
---
### Update uploads
```
$request->upload('binding.key')
```
```php
public function update(UpdateProfileRequest $request, Profile $profile)
{
$request->upload('profile.banner');
$profile->update($request->validated());
return redirect()->route('profiles.show', $profile);
}
```
If the request has a new file for `key`, it deletes the old and uploads the new
If no new file is in the request, it will add the original file path back to `validated()`
So submitting `null` for that `key` will result in the same path / no changes to the model.
It will get the original file path using the `key` & [route model binding](https://laravel.com/docs/routing#route-model-binding), in this scenario.. `profile`
The binding `profile` connects a typehinted model & defined in `Route::` using `{binding}` syntax
---
### Manual update
If you are not using route model binding, you can manually set the path to update.
```php
public function update(UpdateProfileRequest $request, Profile $profile)
{
$request->updateUpload($profile, 'banner');
$profile->update($request->validated());
return redirect()->route('profiles.show', $profile);
}
```
## Parameters
You can specify a few options and override the defaults.
```php
$request->upload('banner', 'uploads', 'public');
```
- `uploads`: is the default path within storage
- `public`: is the default disk within config/filesystems
### Author
[@im_brian_d](https://twitter.com/im_brian_d)