2017-12-17 18 views
1

사용자가 Laravel Authentication을 사용하여 이름, 전자 메일, 암호 및 dob와 같은 자격 증명을 사용하여 자신의 프로필 사진을 업로드 할 수있는 웹 사이트를 만들고 있습니다. 여기Laravel 등록 사진 업로드

protected function create(array $data) 
{ 
    return User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
     'dob' => $data['dob'], 
     'profile_picture' => $data['profile_picture], 
    ]); 
} 

및 내 양식은 다음과 같습니다 :

php artisan make:auth을 수행 한 후, AuthController.php에서이 기능이있다

<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}" enctype="multipart/form-data"> 
    {{ csrf_field() }} 

    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
     <label for="email" class="col-md-4 control-label">E-mail Address</label> 
     <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}"> 

     @if ($errors->has('email')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('email') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> 
     <label for="password" class="col-md-4 control-label">Password</label> 
     <input id="password" type="password" class="form-control" name="password"> 

     @if ($errors->has('password')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('password') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}"> 
     <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label> 
     <input id="password-confirm" type="password" class="form-control" name="password_confirmation"> 

     @if ($errors->has('password_confirmation')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('password_confirmation') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> 
     <label for="name" class="col-md-4 control-label">Fullname</label> 
     <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}"> 

     @if ($errors->has('name')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('name') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group{{ $errors->has('dob') ? ' has-error' : '' }}"> 
     <label for="name" class="col-md-4 control-label">Date of Birth</label> 
     <input id="dateOfBirth" type="date" class="form-control" name="dob"> 

     @if ($errors->has('dob')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('dob') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group{{ $errors->has('profile_picture') ? ' has-error' : '' }}"> 
     <label for="name" class="col-md-4 control-label">Profile Picture</label> 
     <input id="profilePicture" type="file" class="form-control" name="profile_picture"> 

     @if ($errors->has('profile_picture')) 
      <span class="help-block"> 
       <strong>{{ $errors->first('profile_picture') }}</strong> 
      </span> 
     @endif 
    </div> 

    <div class="form-group"> 
     <button type="submit" class="btn btn-primary"> 
      <i class="fa fa-btn fa-user"></i> Register 
     </button> 
    </div> 
</form> 

나는 그것의 확장자를 가진 이미지 파일 이름을 저장하고 싶습니다. 하지만 어떻게해야합니까? 문자열로 파일 이름을 가져 오는 방법, 이미지 파일을 공용 폴더로 이동 한 다음 XAMPP 데이터베이스에 저장하는 방법은 무엇입니까? 그냥 이름을 저장하고 profile_images 폴더에 공용 디렉토리에 업로드 할 경우

+0

protected function create,이 같은 작업을 수행 할 수 있습니다. 현실에서는 DB에 이미지를 저장하지 않습니다! 서버의 해당 이미지에 "문자열"경로 만 저장합니다! – lewis4u

+0

@ lewis4u 예! 그게 내가 원하는거야! 문자열 만이 파일 이름입니다. –

답변

3

는, 당신은 뭔가를 지우려면 그냥

  $request = request(); 

      $profileImage = $request->file('profile_picture'); 
      $profileImageSaveAsName = time() . Auth::id() . "-profile." . 
             $profileImage->getClientOriginalExtension(); 

      $upload_path = 'profile_images/'; 
      $profile_image_url = $upload_path . $profileImageSaveAsName; 
      $success = $profileImage->move($upload_path, $profileImageSaveAsName); 


    return User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
     'dob' => $data['dob'], 
     'profile_picture' => $profile_image_url, 
    ]); 
+0

request() 함수의 출처를 설명해 주시겠습니까? 데이터가 $ data 변수를 통해 함수를 생성하기 위해 전달되고 있으므로 $ request = request();를 이해할 수 없습니다. 암호. –

+0

여기에 요청 도우미를 사용했습니다. ""요청() 요청 함수는 현재 요청 인스턴스를 반환하거나 입력 항목을 얻습니다. $ request = request(); " – PSA

+0

요청 섹션에서이 함수를 찾고 있었지만, 헬퍼 섹션에서 찾을 수 있습니다. –