Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

Uploading file into database laravel fortify

I'm new to laravel and I'm trying to create a simple register form using fortify. The problem is that I am trying to upload a file on the same form and I am struggling to figure out how I can do this in the same controller. I added an extra column on database with varchar type for the file. When I try to submit the form it keeps giving me an error: "BadMethodCallException Method IlluminateHttpUploadedFile::file does not exist." I don't know how to make this work. Please help.

The form:

<form class="multisteps-form__form" id="wizard" enctype="multipart/form-data" method="POST" action="{{route('register')}}">
@csrf
<div class="step-content-field">
        
    <div class="form-inner-area">
        <label>First Name:</label>
        <input type="text" name="firstname" id="firstname" class="form-control "  placeholder="First Name" required>
        <div class="text-danger error" data-error="firstname"></div>
    </div>
    
    <br>
    <div class="form-inner-area">
        <label>Last Name:</label>
        <input type="text" name="lastname" id="lastname" class="form-control "  placeholder="Last Name" required>
        <div class="text-danger error" data-error="lastname"></div>
    </div>
    
    <div class="form-inner-area">
        <label>Email:</label>
        <input type="text" name="email" class="form-control "  id="email" placeholder="Email" required>
        <div class="text-danger error" data-error="email"></div>
    </div>
    
    <div class="form-inner-area">
        <label>Mobile:</label>
        <input type="text" name="mobile" class="form-control " id="mobile" placeholder="Mobile" required>
        <div class="text-danger error" data-error="mobile"></div>
    </div>
    
    <div class="form-inner-area">
        <label>Country:</label>
        <input type="text" name="country" class="form-control "  id="country" placeholder="Country" required>
        <div class="text-danger error" data-error="country"></div>
    </div>
    
    <div class="form-inner-area">
        <label>Password:</label>
        <input type="password" name="password" class="form-control "  id="password" placeholder="Password" required>
    </div>
    
    <div class="form-inner-area">
        <label>Confirm password:</label>
        <input type="password" name="password_confirmation" class="form-control"  id="password_confirm" placeholder="Confirm password" required>
        
    </div>
    <span>The password must be at least 8 characters and contain at least one uppercase character, one number, and one special character.</span>
      <input type="file" name="db_upload" id="upload" class="upload-box" placeholder="Upload csv File">
    </div>
</form>

Here's the CreateNewUser controller:

namespace AppActionsFortify;

use AppModelsUser;
use AppModelsOrder;
use AppNotificationsWelcomeEmailNotification;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;
use IlluminateValidationRule;
use LaravelFortifyContractsCreatesNewUsers;
use Request;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    public function create(array $input)
    {
        Validator::make($input, [
           
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'mobile' => ['required', 'int'],
            'country' => ['required', 'string', 'max:255'],
            'password' => $this->passwordRules(),
            'db_upload' => 'required|mimes:csv,txt,xlx,xls,pdf|max:2048',
            
        ])->validate();

           //File upload
            if($input['db_upload']->file()) {

                $file = $input['db_upload']->file('db_upload');
            
                $fileName = $file->getClientOriginalName();
                $destinationPath = public_path().'/uploads' ;
                $file->move($destinationPath,$fileName);
            }
        
            $user = User::create([
            'firstname' => $input['firstname'],
            'lastname' => $input['lastname'],
            'email' => $input['email'],
            'mobile' => $input['mobile'],
            'country' => $input['email'],
            'password' => Hash::make($input['password']),
            'db_upload' => $file,  
        ]);

            $user->notify(new WelcomeEmailNotification());

                return $user;

question from:https://stackoverflow.com/questions/65848725/uploading-file-into-database-laravel-fortify

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...