在 Laravel 注册中上传个人资料图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35697378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:19:26  来源:igfitidea点击:

Upload Profile Picture in Laravel Registration

phplaravellaravel-5.2laravel-authorization

提问by phenom

Please help me to add upload profile picture on laravel 5.2 registration form. I use Auth for my registration and login, but made some modification there.

请帮我在 laravel 5.2 注册表单上添加上传个人资料图片。我使用 Auth 进行注册和登录,但在那里做了一些修改。

this is my register.blade.php view

这是我的 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

this is my eloquent app\Daftarpegawai.php

这是我雄辩的应用程序\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',
    ];
}

and this is my AuthController.php

这是我的 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']),
        ]);
    }
}

I am new in learning laravel i've tried some methods that i found here with the same problem, but nothing works for me

我是学习 Laravel 的新手我尝试了一些我在这里发现的同样问题的方法,但对我来说没有任何效果

回答by Jason

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
])

回答by Tushar

You can use the below code as your POST request code for uploading avatar to user profile

您可以使用以下代码作为将头像上传到用户个人资料的 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.');
}

Source : Detailed tutorial https://www.5balloons.info/upload-profile-picture-avatar-laravel-5-authentication/

来源:详细教程https://www.5balloons.info/upload-profile-picture-avatar-laravel-5-authentication/

回答by Aushraf

If someone is still facing problem, try using the following codes! Hope it will work...

如果有人仍然遇到问题,请尝试使用以下代码!希望它会起作用...

$avatar = $data['avatar'];
$extension = $avatar->getClientOriginalExtension();
$avatar_name  = time() . '.' . $extension;
$avatar->move(base_path('public/images/avatars/'), $avatar_name);

return User::create([
    'avatar' => $avatar_name,
]);