always return null while uploading file to laravel 7

Viewed 1592

I'm having an issue while uploading file to laravel either its pdf or image always return to null

This is the View

{!! Form::open(['action' => 'TransactionInController@store', 'method' => 'POST', 'autocomplete' => 'off', 
'class' => 'form-horizontal', 'enctype' => 'multipart/form-data']) !!}
            <div class="row">
                {{ Form::label('Device Document', '', ['class' => 'col-sm-2 col-form-label']) }}
                <div class="col-sm-7">
                    {{ Form::file('device_document') }}
                    <p style="color: red;">@error('device_document') {{ $message }} @enderror</p>
                </div>
            </div>
{!! Form::close() !!}

and this is the Controller i use

public function store(Request $request)
{
    $this->validate($request, [
        'device_document' => 'nullable|max:8192|mimes:pdf'
    ]);

    $transactionsin = new TransactionIn;
    $imageName = $request->input('device_document');  
    $request->image->move(public_path('document-image'), $imageName);
    $transactionsin->save();
    
    return redirect('/transactionsin');
}

i know its been asked before and i already try several way to upload file this error.

This is the error message i get while running the code

Call to a member function move() on null

but if i change the code in controller into something more simple like this

public function store(Request $request)
{
    $this->validate($request, [
        'device_document' => 'nullable|max:8192|mimes:pdf'
    ]);

    $transactionsin = new TransactionIn;
    $transactionsin->device_document = $request->input('device_document');
    $transactionsin->save();
    
    return redirect('/transactionsin');
}

it will not return any error message but it will saved as null in the database.

2 Answers

you can access file using file() method not input method and after upload image to get image path using asset() function like this below code

    $transactionsin = new TransactionIn;
    $image= $request->file('device_condition'); 
    //upload imaage
    $image->move(public_path('document-image'), $image->getClientOriginalExtension());
    //asset() function use store path
    $transactionsin->device_document =  asset('document-image/'.$image->getClientOriginalExtension());
    $transactionsin->save();

Use $request->file('device_document') instead of input method to catch a file.

If you would like to get original name of the uploaded file, you may do so using the getClientOriginalName() method

Try this :

public function store(Request $request)
{
    $this->validate($request, [
        'device_document' => 'nullable|max:8192|mimes:pdf'
    ]);

    $transactionsin = new TransactionIn;
    $imageName = $request->file('device_document');
    $imageName->move(public_path('document-image'), $imageName->getClientOriginalName());
    $transactionsin->device_document = $request->file('device_document')->getClientOriginalName();
    $transactionsin->save();
    
    return redirect('/transactionsin');
}

See the official documentation here

Related