Я бы хотел, чтобы пользователь мог загружать изображение в базу данных через форму. Пожалуйста, помогите мне.
register.blade:
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Naam') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-mail') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="adres" class="col-md-4 col-form-label text-md-right">{{ __('Adres') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="adres" type="text" class="form-control @error('adres') is-invalid @enderror" name="adres" value="{{ old('adres') }}" required autocomplete="adres" autofocus>
@error('adres')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="geboortedatum" class="col-md-4 col-form-label text-md-right">{{ __('Geboortedatum') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="geboortedatum" type="date" class="form-control @error('geboortedatum') is-invalid @enderror" name="geboortedatum" value="{{ old('geboortedatum') }}" required autocomplete="geboortedatum" autofocus>
@error('geboortedatum')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="telefoon" class="col-md-4 col-form-label text-md-right">{{ __('Telefoon') }}</label>
<div class="col-md-6">
<input id="telefoon" type="text" class="form-control @error('telefoon') is-invalid @enderror" name="telefoon" value="{{ old('telefoon') }}" autocomplete="telefoon" autofocus>
@error('telefoon')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="gsm" class="col-md-4 col-form-label text-md-right">{{ __('GSM') }}</label>
<div class="col-md-6">
<input id="gsm" type="text" class="form-control @error('gsm') is-invalid @enderror" name="gsm" value="{{ old('gsm') }}" autocomplete="gsm" autofocus>
@error('gsm')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="foto" class="col-md-4 col-form-label text-md-right">{{ __('Foto') }}</label>
<div class="col-md-6">
<input id="foto" type="file" class="form-control @error('foto') is-invalid @enderror" name="foto" value="{{ old('foto') }}" autocomplete="foto" autofocus>
@error('foto')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Wachtwoord') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Herhaal wachtwoord') }}<a style="color:rgb(186, 43, 43)">*</a></label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary" style="background-color:rgb(231, 187, 86); border:none;">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
RegisterController:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(Request $request)
{
$data = $request->all();
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'adres' => ['required', 'string', 'max:255'],
'geboortedatum' => ['required', 'date'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(Request $request)
{
$data = $request->all();
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'adres' => $data['adres'],
'geboortedatum' => $data['geboortedatum'],
'telefoon' => $data['telefoon'],
'gsm' => $data['gsm'],
**'foto' => $request->file('foto')**
]);
}
}
Я не думаю, что в переменной $ request есть файл. Как мне обратиться к файлу, чтобы получить содержимое и сохранить его в базе данных как большой двоичный объект?
Может ли кто-нибудь помочь с этим, чтобы пользователи могли загрузить изображение в базу данных? Спасибо!