noblemfd's avatar

How to retrieve uploaded file in edit view blade

I ham uploading files like excel and images.

I have successfully inserted it. I saved the file name in the table and the file itself in a directly. Where I have a problem is how to retrieve the file in the edit view blade.

Controller

public function update_employee_mid_year_comment(UpdateSelfReviewRequest $request, $id) {
    $goal = Goal::find($id);  

    $goal->employee_mid_year_comment = $request->employee_mid_year_comment;

    if ($request->employee_mid_year_attachment != "") {
        $employee_mid_year_attachment = $request->file('employee_mid_year_attachment');
        $new_name = rand() . '.' . $employee_mid_year_attachment->getClientOriginalExtension();
        $employee_mid_year_attachment->move(public_path('storage/documents/mid_year'), $new_name);
        $goal->employee_mid_year_attachment = $new_name;
    }
    $goal->save();

    DB::commit();

    Session::flash('success', 'Comment is Successfully Updated');
    return redirect()->back();
}

I am using a modal form:

edit.blade


<div class="modal fade" id="edit{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
        <form action="{{route('mid_year_setups.update_employee_mid_year_comment',['id'=>$goal->id])}}" method="post" enctype="multipart/form-data" id="edit_comment-form">
        {{ csrf_field() }}
        <div class="modal-header">
            Update Self-Review Comment
        </div>
        <div class="col-md-12">
            <div class="form-group">
            <label class="control-label">Comment:<span style="color:red;">*</span></label>
            <textarea rows="2" name="employee_mid_year_comment" class="form-control" placeholder="Enter Comment here" value="{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}})}}" required data-validation-required-message="This field is required">{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}}</textarea>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
            <label class="control-label"> Attachment:</label>
            <div class="custom-file">
                <div class="custom-file">
                <input value="{{old('employee_mid_year_attachment',$goal->employee_mid_year_attachment)}}" type="file" name="employee_mid_year_attachment" class="custom-file-input" id="customFile">
                <label class="custom-file-label" for="exampleInputFile">Choose file</label>
                </div>
            </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="submit" id="edit_comment_btn-submit" class="btn btn-success btn-ok">Save</button>
        </div>
        </form>
    </div>
    </div>
</div>

The file name is employee_mid_year_attachment while the file path is: storage/documents/mid_year

When I rendered the edit view blade, it didn't retrieve the attached document. How do I achieve this?

Thanks

0 likes
17 replies
a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@noblemfd

you can not show file in your file input

you need to apply it on this way

	// in your controller function 

	$filePath = storage_path().'/documents/mid_year/'.$goal->employee_mid_year_attachment;
	$fileType = $this->getFileType($filePath);

	return view('view-name', compact('goal', 'filePath', 'fileType'));

	// create this function in your controller or any repo where it could be access 
	protected function getFileType($path) {

		$image_extensions = ['jpg', 'jpeg', 'gif', 'png'];
		$info = pathinfo($path);
		$fileType = $info['extension'];

		if(in_array($fileType, $image_extensions))
		{
		    return 'image';
		}

		return 'other';
	}
	
	// in your blade file 
	<div class="custom-file">
        <div class="custom-file">
        	@if($fileType == 'image')
	        	<img srs="{{ $filePath }}" title="{{ $goal->employee_mid_year_attachment }}" >
	        @else 
	        	<img srs="{{ storage_path('app/file-icon.opg') }}" title="{{ $goal->employee_mid_year_attachment }}" >
	        	<span>{{ $goal->employee_mid_year_attachment }}</span>
	        @endif

        	<input value="{{ old('employee_mid_year_attachment') }}" type="file" name="employee_mid_year_attachment" class="custom-file-input" id="customFile">
        	<label class="custom-file-label" for="exampleInputFile">Choose file</label>
        </div>
    </div>
noblemfd's avatar

@a4ashraf - I got this error:

Undefined index: extension

and this is highlighted:

$fileType = $info['extension'];

a4ashraf's avatar

@noblemfd

maybe your file name or path not correct, make sure your file name and storage path is correct DD the $goal and see what is the file name and check your storage path is a file there or even you can exception handling

it's working on my side and here is the output of a file info array

array:4 [▼
  "dirname" => "/Users/ashraf/Sites/data/storage/app"
  "basename" => "tst.xlsx"
  "extension" => "xlsx"
  "filename" => "tst"
]
noblemfd's avatar

@a4ashraf - Mine is:

/public/storage/documents/mid_year/.$goal->employee_mid_year_attachment
a4ashraf's avatar

@noblemfd

yes, your file path is ok but make sure is there file name is correct in $goal->employee_mid_year_attachment

and also check is this file physically available on this path with the same name

share this dd($goal) output as well

	$filePath = storage_path().'/documents/mid_year/'.$goal->employee_mid_year_attachment;
noblemfd's avatar

@a4ashraf - dd($filePath); gives:

"C:\xampp\htdocs\staffapp\storage/documents/mid_year/1469860112.PNG"

"C:\xampp\htdocs\staffapp\storage/documents/mid_year/1469860112.PNG"
a4ashraf's avatar

@noblemfd

change your file path like this and try it

$filePath = storage_path().'\documents\mid_year\'.$goal->employee_mid_year_attachment;
noblemfd's avatar

@a4ashraf - Error occur.

Unexpected identifier after '\documents\mid_year'.$goal->employee_mid_year_attachment;

noblemfd's avatar

@automica - How do you mean. @automica said I should change it to :

$filePath = storage_path().'\documents\mid_year\'.$goal->employee_mid_year_attachment;
automica's avatar

@noblemfd yep but your error:

Unexpected identifier after '\documents\mid_year'.$goal->employee_mid_year_attachment;

suggests otherwise. (missing last slash)

noblemfd's avatar

@automica - The slash is there, I don't know why I didn't include it. See it

$filePath = storage_path().'\documents\mid_year'.$goal->employee_mid_year_attachment;

noblemfd's avatar

@automica - I have added the slash:

 $filePath = storage_path().'\documents\mid_year\'.$goal->employee_mid_year_attachment;

and the error is still there. See the the error:

unexpected: identifier after: String ''\documents\mid_year'.$goal->employee_mid_year_attachment;

automica's avatar

@noblemfd it looks like your last slash is being ignored. can you remove both the start and end quote marks and re-add them again as it looks like you may have an odd character especially if you've copied it in from @a4ashraf comment above.

noblemfd's avatar

@automica - When I made it as:

$filePath = storage_path().\documents\mid_year\.$goal->employee_mid_year_attachment;  

I got this error:

syntax error, unexpected '.', expecting identifier (T_STRING)

automica's avatar

@noblemfd you need to add the quotes again. It looked like they were mismatched though.

Please or to participate in this conversation.