Пожалуйста, помогите мне добавить изображение профиля загрузки на форму регистрации laravel 5.2.
Я использую Auth для своей регистрации и входа в систему, но внес некоторые изменения там.
это мой просмотр register.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-primary">
<div class="panel-heading">Pendaftaran Akun Baru</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<!-- start of nip -->
<div class="form-group{{ $errors->has('nip') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Nomor Induk Pegawai</label>
<div class="col-md-6">
<input type="text" class="form-control" name="nip" value="{{ old('nip') }}">
@if ($errors->has('nip'))
<span class="help-block">
<strong>{{ $errors->first('nip') }}</strong>
</span>
@endif
</div>
</div>
<!-- end of nip -->
<!-- start of nama -->
<div class="form-group{{ $errors->has('nama') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Nama</label>
<div class="col-md-6">
<input type="text" class="form-control" name="nama" value="{{ old('nama') }}">
@if ($errors->has('nama'))
<span class="help-block">
<strong>{{ $errors->first('nama') }}</strong>
</span>
@endif
</div>
</div>
<!-- end of nama -->
<!-- start of email -->
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Alamat E-mail</label>
<div class="col-md-6">
<input 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>
<!-- end of email -->
<!-- start of password -->
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<!-- end of password -->
<!-- start of konfirmasi -->
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Konfirmasi Password</label>
<div class="col-md-6">
<input 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>
<!-- end of konfirmasi -->
<!-- start of foto -->
<div class="form-group{{ $errors->has('avatar') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">F o t o</label>
<div class="col-md-6">
<div id="kv-avatar-errors" class="center-block" style="width:800px;display:none">
</div>
<div class="kv-avatar" style="width:200px">
<input id="avatar" name="avatar" type="file" class="file-loading">
</div>
</div>
</div>
<!-- end of foto -->
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Daftar
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
это мое красноречивое приложение \ Daftarpegawai.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Daftarpegawai extends Authenticatable {
protected $fillable = [
'nip', 'nama', 'email', 'password', 'foto',
];
protected $hidden = [
'password', 'status', 'remember_token',
];
}
и это мой AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Daftarpegawai;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
protected function validator(array $data)
{
return Validator::make($data, [
'nip' => 'required|max:20',
'nama' => 'required|max:255',
'avatar' => 'mimes:jpg',
'email' => 'required|email|max:255|unique:daftarpegawais',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return Daftarpegawai::create([
'nip' => $data['nip'],
'nama' => $data['nama'],
'foto' => $data['avatar'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Я новичок в изучении Laravel
Я пробовал некоторые методы, которые я нашел здесь с той же проблемой, но у меня ничего не работает
if($data->hasFile('avatar'))
{
$destinationPath = 'path/to/upload/the/file'
$imgName = 'yourimagename.jpg'
$data->avatar->move($destinationPath, $imgName)
$path = $destinationPath . '/' . $imgName
}
DaftarPegawai::create([
'foto' => $path
])
Вы можете использовать приведенный ниже код в качестве кода запроса POST для загрузки аватара в профиль пользователя.
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::user();
$avatarName = $user->id.'_avatar'.time().'.'.request()->avatar-
>getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
Источник: подробное руководство https://www.5balloons.info/upload-profile-picture-avatar-laravel-5-authentication/