Spatie媒体库未上传

时间:2018-08-30 08:37:09

标签: laravel file-upload eloquent spatie

我的注册分为3部分,在第二部分中,用户必须上传图像。我在上传图片时遇到问题。有时它有用,有时却没有。

这是注册控制器代码。我在用户模型中实现了所有特征。照片在步骤2中。

    //Step 1
    public function youthRegistrationStep1(){
      return view('auth.youth-register-step1');
    }

    public function postYouthRegistrationStep1(Request $request){

      $validatedData = $request->validate([
        'username' => 'unique:users|required',
        'phone' => 'unique:users|required',
        'date_of_birth' => 'required',
        'password'=> 'required',
        'company_code' => 'nullable',
        'gender' => 'required',
        'country_id' => 'required'
      ]);

      if(empty($request->session()->get('user'))){
            $user = new User();
            $user->fill($validatedData);
            $request->session()->put('user', $user);
        }else{
            $user = $request->session()->get('user');
            $user->fill($validatedData);
            $request->session()->put('user', $user);
        }

        return redirect('/youth-registration/step2');

    }

这是上传照片的地方

    //Step 2
    public function youthRegistrationStep2(){
        return view('auth.youth-register-step2');
    }

    public function postYouthRegistrationStep2(Request $request){
      $validatedData = $request->validate([
        'profile_image' => 'required|file'
      ]);

      if(empty($request->session()->get('user'))){
            $user = new User();
            $user->addMediaFromRequest('profile_image')->toMediaCollection('profile_image');
            $request->session()->put('user', $user);
        }else{
            $user = $request->session()->get('user');
            $user->addMediaFromRequest('profile_image')->toMediaCollection('profile_image');
            $request->session()->put('user', $user);
        }


      return redirect('/youth-registration/step3');
    }

    //Step 3
    public function youthRegistrationStep3(){
      return view('auth.youth-register-step3');
    }

    //Posts all data and creates user.
    public function youthregister(Request $request)
    {
        $validatedData = $request->validate([
          'terms_and_privacy_condition' => 'required',
          'allowed_to_contact_condition' => 'required',
          'term_condition_3' => 'required',
          'photo_consent' => 'required'
        ]);

        $user = $request->session()->get('user'); //Data from all 3 pages.

        //Additional Data
        $user->confirmation_code = md5(uniqid(mt_rand(), true));
        $user->confirmed = 1;
        $user->password = bcrypt($request->password);
        $user->status = 1;
        $user->term_condition_submitted_on = Carbon::now();
        $user->user_ip = request()->ip();
        $user->terms_and_privacy_condition = 1;
        $user->allowed_to_contact_condition = 1;
        $user->term_condition_3 = 1;
        $user->photo_consent = 1;
        $user->account_verified = 0;

        $user->save();

        $message_text = "Dear $user->username, Thank you for registering on Turn2me.org. A counsellor will be in touch to arrange an initial Video counsultation. Once you are verified you will have full user of our services.";
        \SmsService::SendSMS($user->phone, $message_text);

        $user->assignRole('user');

        auth()->login($user);


        return redirect('/dashboard');
    }

我的看法是这个

<form action="/youth-registration/step2" id="signupForm" method="POST" class="register-form" enctype="multipart/form-data">
      {{ csrf_field() }}

      <h4 class="step-heading btn-margin">Step 2 - Verification</h4>


      <div class="input--file youth-photo">
        <h3 class="input--file__title">Photo of Your Passport </h3>
        <div class="user-icon--uploader-wrapper">
          <div class="previews"></div>
          <input type="file" name="profile_image" id="profile_image">
        </div>
          <span class="personalphoto">Only a photo of your passport displaying  your photo and information will be permitted.</span>
      </div>

      <div class="youth-registration__steps">
        <div class="input--submit">
          <button type="submit" class="btn-pill--filled complete-step">Go To The Next Step</button>
        </div>
        <div class="youth-registration__steps-list">
          <p>
            Step 2 of 3 <span>Completed</span>
          </p>
          <div class="youth-registration__checkboxes">
            <span class="complete-step"></span>
            <span id="youth-registration-complete"></span>
            <span></span>
          </div>
        </div>
      </div>
    </form>

0 个答案:

没有答案