U
U
Uglik2016-04-14 13:49:49
Laravel
Uglik, 2016-04-14 13:49:49

How to display avatar from Storage laravel?

Hello.
In the Profile Controller, when updating the profile, I write the avatar to Storeg:

/**
     * Update user profile
     *
     * @param \App\User $user
     * @param \App\Http\Requests\ProfileUserUpdateRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(User $user, UserUpdate $request)
    {

        if($request->hasFile('avatar'))
        {
            
            // Get user avatar image request
            $image = $request->file('avatar');
            $extension = $image->getClientOriginalExtension();

            // Resize image user avatar
            $size = \Config::get('image.avatar_size');
            $avatar = Image::make($image)->fit($size)->encode($extension);

            // Save user avatar image
            $path = $user->username . '/avatar.' . $extension;
            Storage::disk('users')->put($path, $avatar);
            
        }

        $user->update($request->all());
        return redirect()->route('profile.edit', $request['username']);
    }

Data comes from the form and is checked for validity.
From the request, we get the image file and let's resize.
Is it worth storing any data in the database at all, or can you just do without it?
How can I display this image in the user profile now? Logically, the path is formed according to the username data.
Options:
- write a function in the model to which username will be passed and return response storage
- you can somehow implement it through the controller
- create a separate Route for this matter
How would be more correct? And how to structure the code data in general. We have 3 entities: the path to the image, write the image, get the image. What would in the future refer to each entity separately from the proposition. But how then, in this case, to get the correct image extension and pass username everywhere?
PS And another question, how to implement that the flash message would be displayed only if something in the data is changed, and not every time the update button is pressed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-04-14
@DJZT

About flash - check for changes. And if something has changed, then do it There is a getOriginalredirect()->with('success', '...')
model method Try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question