E
E
EVOSandru62017-12-15 14:02:01
Java
EVOSandru6, 2017-12-15 14:02:01

How in Laravel 5.x to encapsulate file uploads for different entities, or at least within different actions in the controller?

Good afternoon!
There is a form _form.blade.php that is pulled by the view blade: create.blade.php and update.blade.php

...
    {{ Form::file('photo') }}
    <img src="/storage/images/articles/{{ $article->photo }}" style="height:50px" alt="">
...

The picture flies along with the post form. The method has outputs, which I will print out below:
class ArticlesController extends Controller
{
    public function update(ArticleRequest $request, Article $article)
    {
        $request = $this->saveImage($request);

        dump($request->all());

        dump($request->photo);

        $article->update($request->all());

        dd($article->photo);

        $this->syncTags($article, $request->input('tag_list'));

        return redirect()->back();
    }

private function saveImage(ArticleRequest $request)
    {

        if ($request->hasFile('photo')) {

            $file = $request->file('photo'); // ->isValid()

            $fileNameWithExt = $file->getClientOriginalName();
            $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
            $extension = $file->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            $file->storeAs('public/articles', $fileNameToStore);

        } else {
            $fileNameToStore = 'noimage.jpg';
        }

        $request->photo = $fileNameToStore;

        return $request;

    }

}

dump and dd output
1)
array:7 [▼
"_method" => "PATCH"
"_token" => "YPmvNLjhmoqKdxIt7RL2YbviiIjcfpEdqe0JW7ms"
"name" => "Sleep snow"
"content" => "
Sleep snow in winter night with bears and foxes"
"published_at" => "2017-12-14"
"tag_list" => array:1 [▶]
"photo" => UploadedFile {#238 ▶}
]
2)
"logoct_1513334576.png"
3)
UploadedFile {#238 ▼
-test: false
-originalName: "logoct.png"
-mimeType: "image/png"
-size:55490
-error: 0
#hashName: null
path: "/tmp"
filename: "phpulvqiE"
basename: "phpulvqiE"
pathname: "/tmp/phpulvqiE"
extension: ""
realPath: "/tmp/phpulvqiE"
aTime: 2017-12-15 10:42:56
mTime: 2017-12-15 10:42:56
cTime : 2017-12-15 10:42:56
inode: 21630383
size: 55490
perms: 0100600
owner: 33
group: 33
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}

The picture itself is loaded, the problem is that $article->photo remains empty.
In the saveImage method, I wanted to assign to the photo propertythe name of the image, but the $article->photo property ends up with an UploadedFile object.
Why is it so? I return $request in the saveImage method , where the photo property is overridden.
I did not find a single example where this loading logic would be encapsulated.
In Yii2 framework such things were fed to behaviors. In Laravel, I suppose that perhaps middleware should be somehow involved for such cases - if at all, load the image and determine the name for the entity when updating and adding different entities (Also deleting the image when deleting the entity).
The $fillable property contains the photo element .
There is also a rule for the picture:
class ArticleRequest extends FormRequest{
...
public function rules()
    {
        return [
...
            'photo'=>'image|nullable|max:1999'
...
        ];
    }
...
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2019-10-04
@Web__Nikita03

BigDecimal will save you. And yes, with a double this is all the time. The example is not at the cash desk, but it is indicative.

Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
    .setScale(3, RoundingMode.HALF_UP)
    .doubleValue();

A
Andreo, 2017-12-19
@EVOSandru6

I wouldn't do that.
The best solution in my opinion would be just feeding the UploadedFile object into the model being used. And in the model itself, use the setter of this attribute to intercept and convert the value.
It will look like this in the model:

public function setPhotoAttribute($value) {
    if ($value instanceOf UploadedFile) {
            $fileNameWithExt = $value->getClientOriginalName();
            $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
            $extension = $value->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            $file->storeAs('public/articles', $fileNameToStore);
            return $this->attributes['photo'] = $fileNameToStore;
    }
}

The source needs to be checked. Done by hand :)

H
hakkol, 2017-12-15
@hakkol

To change the property of $request you need to use merge
$request->merge(['photo' => $fileNameToStore]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question