0

I have a problem with getting of uploaded image, because when I posted my form with picture, after reloading of page the input asked to choose the file again, how I can get choosed image? So If I will post this form again, It will be empty, because I did not choose file

<div class="col-sm-12 col-lg-12 my_form">
                    <label class="col-sm-4 control-label  ">Picture</label>
                    <div class="col-sm-8">
                            <input class="form-control" type="file" name="picture" accept=".jpeg, .png, .jpg">
                            <img src="{{picture}}">
                    </div>
                </div>
Aleks
  • 147
  • 10
  • Can you make it more clear why you need to submit the form more than one time? – Hussam Adil Dec 09 '19 at 08:17
  • @HussamAdil For example: CREATE PROFILE with photo, some times later I want to change name of user, and when I submit the PHOTO value will null, because I do not to choose new photo – Aleks Dec 09 '19 at 08:22
  • I think we should tell laravel If image is not empty put the rules otherwise do nothing. `$this->validate($request, [ 'image' => 'sometimes|nullable', ]);` for more read [https://laravel.com/docs/5.8/validation#conditionally-adding-rules] – Hussam Adil Dec 09 '19 at 08:27
  • Yeah I thought about it but if I want that my profile will withought photo? – Aleks Dec 09 '19 at 08:30
  • your image profile is nullable or required? – Hussam Adil Dec 09 '19 at 08:31
  • @HussamAdil Nullable, so sometimes I need a photo sometimes not – Aleks Dec 09 '19 at 08:34
  • you need the image if the user selected or if the user has no profile image? so when you need the image? – Hussam Adil Dec 09 '19 at 08:38
  • @HussamAdil I need image when I want, not everytimes. – Aleks Dec 09 '19 at 08:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203891/discussion-between-aleks-and-hussam-adil). – Aleks Dec 09 '19 at 09:10

1 Answers1

0

You can just check if the $image == null then it will require the image but if the $image != null just display the uploaded image and don't require the input.

In your controller remove the required validation.

$request->validate([
            'name'              =>  'required',
            'profile_image'     =>  'image|mimes:jpeg,png,jpg,gif|max:2048'
        ]);

In this code it will not overwrite the image with null.

Luigel
  • 331
  • 1
  • 7
  • Thank you, but It is not true solution. If I reload page, I need that will choosed file and not to choose again – Aleks Dec 09 '19 at 07:03
  • Can you post the snippet of your template? – Luigel Dec 09 '19 at 07:06
  • I changed my post – Aleks Dec 09 '19 at 07:11
  • @Aleks I also need to know the controller in `store` and `index` or `show` – Luigel Dec 09 '19 at 07:15
  • Controller is like here https://www.larashout.com/laravel-image-upload-made-easy – Aleks Dec 09 '19 at 07:16
  • I updated my code. It will error because the request was validated and profile_image is null so it will not save the updated user – Luigel Dec 09 '19 at 07:26
  • Sorry, but I think you did not understand my question. My code is complete -good. But how I can get path after reload? Example: WHEN WE TYPE NAME: alice, after reloading page name stayed ALICE. But if I choosed file and reload page the file path is NULL so HOW I CAN GET FILE PATH AFTER RELOADING PAGE – Aleks Dec 09 '19 at 07:32
  • I need a snippet of the controller that returns the view of the form. How did you get the `$picture` data – Luigel Dec 09 '19 at 07:35
  • My controller is absolutely the same like larashout.com/laravel-image-upload-made-easy – Aleks Dec 09 '19 at 07:38
  • I think you need to change your `src` attribute to `{{ asset(auth()->user()->image) }}` – Luigel Dec 09 '19 at 07:51
  • Yeah, I did it. But it is only to show image and after submit it will be empty because I will not new picture – Aleks Dec 09 '19 at 07:55