Whats the best way to pass a value into a controller in laravel

35 Views Asked by At

I'm trying to make a profile page with the ability to upload different documents. At the moment I have 3 different modals where the user can upload 3 different types of documents. I want to make this process as effective as possible so I was thinking of naming the 3 documents a specific name such as UserCV and then accessing the documents on the front end using this name and the user`s ID. But I was just wondering what the best way for me to pass this name value from my modal to my controller. As I was planning on making an input field that can't be edited on the modal but feel like there must be a better way.

The current way I'm doing it :

Modal :

<div class="modal fade" id="IDModal" tabindex="-1" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Upload Id</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
            <form action="{{ route('id.upload.post') }}" method="POST" enctype="multipart/form-data">
            @csrf
                <div class="row">
                    <div class="input-group mb-3">
                        <input type="text" id="document_name" name="doc_name" value="UserIDCard" readonly>
                        <input type="file" class="form-control" id="idInput" name="id_upload">
                        <button type="submit" class="btn btn-secondary input-group-text" for="idInput">Upload</button>
                    </div>
            
                </div>
            </form>
        </div>
</div>

Controller :

public function uploadID(Request $request){
    $request->validate([
        'id_upload' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $file = $request->file('id_upload');

    if ($path = $request->file('id_upload')->storePublicly('images/user-documents', 's3'))
    {
        $request->user()->documents()->create([
            'name' => $request->doc_name,
            'path' => Storage::url($path),
            'bytes' => $file->getSize()
        ]);

        $message = 'You have successfully upload image.';
        $type = 'success';
    }
    else
    {
        $message = 'Sorry - something went wrong with the file upload';
        $type = 'warning';
    }

    return redirect()->back()
        ->with('message', $message)
        ->with('message-type', $type);
}
0

There are 0 best solutions below