I have a custom regitration form where I need (users telephone number and company id) as well as email and password.
The telephone number and company id will get an api call to verify its existence in the comapny directory. If it passes then I need to allow User model to make the registration but if it fails then return a fail.
Q: where in the chain of registration should I make this api call to allow/reject the registration?
my \resources\views\auth\register.blade.php looks like this
<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="companyid" class="col-md-4 col-form-label text-md-right">Innovations ID</label>
<div class="col-md-6">
<input id="companyid" type="text" class="form-control" name="companyid" value="" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="telephonenumber" class="col-md-4 col-form-label text-md-right">Zip Code</label>
<div class="col-md-6">
<input id="telephonenumber" type="text" class="form-control" name="telephonenumber" value="" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</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="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</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">{{ __('Confirm Password') }}</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">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
here is my controller app\Http\Controllers\Auth\RegisterController.php
I would think this is where I would add my api call but not sure how to go about it.
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;
class RegisterController extends Controller{
use RegistersUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct(){
$this->middleware('guest');
}
protected function validator(array $data){
return Validator::make($data, [
'companyid' => ['required', 'string', 'max:255'],
'telephonenumber' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
return User::create([
'companyid' => $data['companyid'],
'telephonenumber' => $data['telephonenumber'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
this is my user model app\User.php
protected $fillable = [
'companyid', 'telephonenumber', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
You need to do it in the create method of the RegisterController.
You can make api calls within create method using the Http facade. But you need to have guzzle as dependency in your project. You can pull it using composer.
composer require guzzlehttp/guzzle
Then you can make the api calls
protected function create(array $data)
{
//Remember to import the use statement
//use Illuminate\Support\Facades\Http; at top
$response = Http::get('http::/example.com');
//Need to stop further execution by throwing an exception
//If the verification via api call fails
//abort_unless($response->verified = true, 419);
return User::create([
'companyid' => $data['companyid'],
'telephonenumber' => $data['telephonenumber'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Laravel docs: https://laravel.com/docs/8.x/http-client#making-requests
Related
I have tried to add to do some modification to Registration Controller. I want to add some users record to a different table but I can't get it right. I'm able to capture all the field using dd($data). But I am unable to save the data to the userRecord Table.
In Registration controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'phoneno' => ['required', 'string', 'min:11']
'areas' => ['required', 'string']
]);
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->attachRole('user');
$userRecord = new UserRecords();
$userRecord->phoneno = $data->phoneno;//additional fields
$userRecord->email= $data->email;
$userRecord->areas= $data->areas;//additional fields
$userRecord->save();
}
My model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class userRecords extends Model
{
protected $fillable = [
'name', 'email','phoneno','location',
];
}
My View
#extends('layouts.app')
#section('content')
<div class="container mt-4">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header bg-info text-white">{{ __('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">{{ __('Name') }}</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="name" class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
<div class="col-md-6">
<input id="phoneno" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</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="name" class="col-md-4 col-form-label text-md-right">{{ __('Your Location') }}</label>
<div class="col-md-6">
<select name="areas" id="areas" class="form-control">
#foreach($areas as $item)
<option value="{{ $item->area }}">{{ $item->area}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</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">{{ __('Confirm Password') }}</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">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Trying to get property 'phoneno' of non-object
$data is an array. But, you get values of $data as object. You should change $data->phoneno into $data['phoneno'], $data->email into $data['email'], $data->areas into $data['areas'].
2.
But not sure whether it will throw another error since I wanted to avoid using Auth tables
I think you can open another topic to clarify this.
A few things you need to set correct
Your model is userRecords so when newing up an instance new userRecords
$data is an array so access properties/elements with array syntax $data['email']
You need to return $user from create method
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->attachRole('user');
$userRecord = new userRecords();
$userRecord->phoneno = $data['phoneno'];//additional fields
$userRecord->email= $data['email'];
$userRecord->areas= $data['areas'];//additional fields
$userRecord->save();
return $user;
}
I have 3 users
Customer
Restaurant
Rider
Am trying to register the rider via default routes so I can login again:
<form method="POST" action="{{ route('register') }}">
#csrf
<h4 class="text-light-black fw-600">Create your account</h4>
<input type="text" name="type" value="rider">
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">First name</label>
<input type="text" name="first_name" class="form-control form-control-submit"
placeholder="First Name" required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Last name</label>
<input type="text" name="last_name" class="form-control form-control-submit" placeholder="Last Name"
required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Username</label>
<input type="text" name="username" class="form-control form-control-submit" placeholder="Username"
required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">CNIC</label>
<input type="text" name="cnic" class="form-control form-control-submit" placeholder="CNIC" required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Ride Number</label>
<input type="text" name="ride_number" class="form-control form-control-submit"
placeholder="Ride Number" required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Ride Chassis</label>
<input type="text" name="chassis_number" class="form-control form-control-submit"
placeholder="Ride Chassis" required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Password</label>
<input type="password" name="password" class="form-control form-control-submit"
placeholder="Password" required>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="form-group">
<label class="text-light-white fs-14">Confirm Password</label>
<input type="password" name="password_confirmation" class="form-control form-control-submit"
placeholder="Confirm Password" required>
</div>
</div>
<div class="col-12">
<div class="form-group">
<label class="text-light-white fs-14">Email</label>
<input type="email" name="email" class="form-control form-control-submit" placeholder="Email I'd" required>
</div>
<div class="form-group">
<label class="text-light-white fs-14">Address</label>
<input type="text" name="address" class="form-control form-control-submit" placeholder="Address" required>
</div>
<div class="form-group">
<button type="submit" class="btn-second btn-submit full-width">Create your account</button>
</div>
<div class="form-group text-center"></div>
<span class="text-light-black fs-12 terms">By creating your Munchbox account, you agree to the Terms of Use and Privacy Policy.</span>
</div>
</div>
</form>
Here's is my the create method
protected function create(array $data)
{
dd($data);
if ($data['type']=='rider')
{
$user= Rider::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'password' => Hash::make($data['password']),
'address' => $data['address'],
]);
}else
{
$user = User::create([
'name' => $data['name'],
'password' => Hash::make($data['password']),
'email' => $data['email'],
]);
}
return $user;
}
what am trying to do is when a new rider wants to register themself he can, so he can also use the default login laravel routes and methods
but am failed to do that the page refresh on the same page and the rider doesn't register in the rider table.
https://pusher.com/tutorials/multiple-authentication-guards-laravel
example:
// app/Rider.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Rider extends Authenticatable
{
use Notifiable;
protected $guard = 'rider';
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
// app/Restaurant.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Restaurant extends Authenticatable
{
use Notifiable;
protected $guard = 'restaurant';
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
you have to declare your guards
// config/auth.php
<?php
[...]
'guards' => [
[...]
'rider' => [
'driver' => 'session',
'provider' => 'riders',
],
'restaurant' => [
'driver' => 'session',
'provider' => 'restaurants',
],
],
[...]
and declare your providers
// config/auth.php
[...]
'providers' => [
[...]
'restaurants' => [
'driver' => 'eloquent',
'model' => App\Restaurant::class,
],
'riders' => [
'driver' => 'eloquent',
'model' => App\Rider::class,
],
],
[...]
I set up a new app in laravel and created the out of the box authentication with php artisan make:auth . I needed to add some additional fields to the forms and followed a tutorial (which I can't even find again) and re ran the migrations. The new fields went to the database and all looks good. However, when I try to register, the form does not submit to the database, it does not redirect, and many of the fields get cleared out.
I found one article on stack that mentioned my form view might be messed up with the register button so I've been making sure all my divs are closed and whatnot but it's not fixing it. I also thought the issue may be related to my radio buttons and check boxes so I removed those to test it without and still had the same issue.
my register controller:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
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 = '/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(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:15'],
'program' => ['required'],
'disability'=>['string', 'max:255'],
'terms' => ['required'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'program' =>$data['program'],
'disability'=>$data['disability'],
'terms' => $data['terms'],
'password'=> Hash::make($data['password']),
]);
}
}
below is my form (sorry for sharing the whole thing)
<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">{{ __('Name') }}</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 Address') }}</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="phone number"
class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
<div class="col-md-6">
<input id="phone number" type="text"
class="form-control #error('phone number') is-invalid #enderror"
name="phone number" value="{{ old('phone') }}" required autocomplete="phone"
autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="program"
class="col-md-4 col-form-label text-md-right">{{ __('Program') }}</label>
<div class="col-md-6 ">
<div class="form-inline"><input id="veterans" type="radio" class="form-inline"
name="program" value="Veteran Program ($35.00/month)"
{{ (old('program')== 'veteran') ? 'checked':''}}>
Veteran Program ($35.00/month
</div>
<div class="form-inline"><input id="masters" type="radio" class="form-inline"
name="program" value="Masters Program ($50.00/month)"
{{ (old('program')== 'masters') ? 'checked':''}}>
Masters Program ($50.00/month)
</div>
<div class="form-inline"><input id="adaptive" type="radio" class="form-inline"
name="program" value="Non-Veteran Adaptive Program ($50.00/month)"
{{ (old('program')== 'adaptive') ? 'checked':''}}> Masters Adaptive Program
($50.00/month)
</div>
#error('program')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="type"
class="col-md-4 col-form-label text-md-right">{{ __('Disability Type (If applicable)') }}</label>
<div class="col-md-6">
<input id="disability" type="text" class="form-control" name="disability"
value="{{ old('disability') }}" autofocus>
</div>
</div>
<div class="form-group row">
<label for="waiver"
class="col-md-4 col-form-label text-md-right">{{ __('Terms and Conditions') }}</label>
<div class="col-md-6 ">
<div class="form-inline"><input id="waiver" type="checkbox" class="form-inline"
name="waiver" value="waiver" {{ (old('waiver')== '1') ? 'checked':''}}> By
checking this box, I agree to the ORCA terms & conditions. </div>
#error('waiver')
<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">{{ __('Password') }}</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">{{ __('Confirm Password') }}</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">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I expect it to create the account, login and redirect. Unfortunately, it's not throwing any error messages which is making it more difficult to troubleshoot.
I tried to comment but I can't yet, at first please check names of the phone and terms inputs, cause for the phone input you put "phone number" at name attribute, and for the terms input, you put "waiver" in name attribute,
can you please share the whole controller after
The disability field needed to be update to allow it to be blank. Added 'nullable' to the validator function on the register controller and it worked. Also need to update that column in the migration so the database doesn't error.
I have generated user auth through make:auth, which doesn't have edit details field, now I want to edit details such as email and contact, and others leave as disabled. I have tried to copy code from RegisterController i just changed create method to update method, If you have ready template for edit details please share me, I have searched many ready templates but not found, I just want template, which will be compatible with generated Auth or solution to my problem, because now it is not updating the details
1) View: edit_profile.blade.php
<form method="POST" action="/profile">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" disabled type="text" class="form-control" value='{{$user->name}}' name="name">
</div>
</div>
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Student Number') }}</label>
<div class="col-md-6">
<input id="username" disabled type="text" class="form-control" name="username" value="{{$user->username}}">
</div>
</div>
<div class="form-group row">
<label for="age" class="col-md-4 col-form-label text-md-right">{{ __('Age') }}</label>
<div class="col-md-6">
<input id="age" disabled type="text" class="form-control" name="age" value="{{$user->age}}"
required> #if ($errors->has('age'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('age') }}</strong>
</span> #endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{$user->email}}"
required> #if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span> #endif
</div>
</div>
<div class="form-group row">
<label for="contact" class="col-md-4 col-form-label text-md-right">{{ __('Contact Number') }}</label>
<div class="col-md-6">
<input id="contact" type="text" class="form-control{{ $errors->has('contact') ? ' is-invalid' : '' }}" name="contact" value="{{$user->contact}}"
required> #if ($errors->has('contact'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('contact') }}</strong>
</span> #endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Update Details') }}
</button>
</div>
</div>
</form>
2) Controller: ProfileController
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use Image;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Carbon;
class ProfileController extends Controller
{
public function profile(){
return view('pages.profiles.profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request){
// Handle the user upload of avatar
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->crop(300, 300)->save( public_path('/storage/images/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('pages.profiles.profile', array('user' => Auth::user()) );
}
protected function validator(array $data)
{
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
'contact' => 'numeric|digits_between:7,15',
]);
}
public function edit(){
return view('pages.profiles.edit_profile', array('user' => Auth::user()) );
}
public function update(array $data){
return User::update([
'email' => $data['email'],
'contact' => $data['contact'],
]);
}
}
Routes
//User Profile
Route::get('/profile', 'ProfileController#profile');
Route::post('profile', 'ProfileController#update_avatar');
Route::get('/profile/edit', 'ProfileController#edit');
Route::post('profile/edit', 'ProfileController#update');
There are a few issues that I've noticed.
You're ProfileController#update method accepts an array but it won't be getting passed an array.
You're not calling update on the authenticated user.
You're posting to /profile which looking at your routes if for updating the avatar and not the user data.
Change your form to be:
<form method="POST" action="/profile/edit">
Change your update method to:
public function update(Request $request)
{
$data = $this->validate($request, [
'email' => 'required|email',
'contact' => 'required',
]);
auth()->user()->update($data);
return auth()->user();
}
Documentation for Laravel's Validation
public function update(Request $request)
{
//check validation
Auth::user()->update($request);
return true;
}
I am trying to do registration with user profile picture upload.(I am forced to do it this way)
I created the migration like this:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nom');
$table->string('prenom');
$table->string('type')->default('visiteur');
$table->boolean('confirme')->default(false);
$table->string('email')->unique();
$table->string('password');
$table->string('photo_url')->default('default_photo_profile.jpg');
$table->rememberToken();
$table->timestamps();
});
the create function :
$request = request();
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension();
$path = $request->file('photo')->storeAs('images', $fullname);
}
return User::create([
'nom' => $data['nom'],
'prenom' => $data['prenom'],
'email' => $data['email'],
'photo_url' => $fullname,
'password' => Hash::make($data['password']),
]);
}
and the form for the file field is like this:
<div class="form-group">
<label for="photo_url">Photo profile</label>
<input type="file" name="photo" class="form-control-file" id="photo_url">
</div>
everything is working fine except the photo_url field, it always sets the default value in the migration and not the value I set in the create function.
$fullname is initiated and already declared.
the entire form :
<form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label for="nom" class="col-md-4 col-form-label text-md-right">Nom</label>
<div class="col-md-6">
<input id="nom" type="text" class="form-control{{ $errors->has('nom') ? ' is-invalid' : '' }}" name="nom" value="{{ old('nom') }}" required autofocus>
#if ($errors->has('nom'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('nom') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="prenom" class="col-md-4 col-form-label text-md-right">Prénom</label>
<div class="col-md-6">
<input id="prenom" type="text" class="form-control{{ $errors->has('prenom') ? ' is-invalid' : '' }}" name="prenom" value="{{ old('prenom') }}" required autofocus>
#if ($errors->has('prenom'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('prenom') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Email</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Mot de pass</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
#if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Mot de pass confirmation</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<label for="photo_url">Photo profile</label>
<input type="file" name="photo" class="form-control-file" id="photo_url">
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Envoyer
</button>
</div>
</div>
</form>
What is the problem?
Assuming you have a value for $photo_url, make sure you have 'photo_url' in your $fillables.
When you have $fillables, it only inserts (via User::create) what has in that array, otherwise it doesn't submit for that variable.
Your $fillables should look like this:
$fillables = ['nom','prenom','type','confirme','email','password','photo_url'];
Add 'photo' in $fillable array in User model:
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'photo_url',
];
so according to your code in your model you have to add the photo_url in $fillable array like below.
$fillables = ['nom','prenom','type','confirme','email','password','photo_url'];
okay now there are 3 ways to do it with $fillable is first way and you are doing it right now.
2nd way:
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension();
$path = $request->file('photo')->storeAs('images', $fullname);
}
else
{
$fullname = "default_photo_profile.jpg";
}
return User::create([
'nom' => $data['nom'],
'prenom' => $data['prenom'],
'email' => $data['email'],
'photo_url' => $fullname,
'password' => Hash::make($data['password']),
]);
and in your migration change this $table->string('photo_url')->default('default_photo_profile.jpg'); to $table->string('photo_url');
3rd way:
$fullname = "default_photo_profile.jpg";
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$fullname=$data['nom'].'_'.date("Y-m-d",time()).'.'.$file->getClientOriginalExtension();
$path = $request->file('photo')->storeAs('images', $fullname);
return User::create([
'nom' => $data['nom'],
'prenom' => $data['prenom'],
'email' => $data['email'],
'photo_url' => $fullname,
'password' => Hash::make($data['password']),
]);
}
return User::create([
'nom' => $data['nom'],
'prenom' => $data['prenom'],
'email' => $data['email'],
'photo_url' => $fullname,
'password' => Hash::make($data['password']),
]);
}
okay these are the ways to do it. i would prefer first and second way 3rd one is lengthy.
Note: for 2nd and 3rd case you have to change your migration from this $table->string('photo_url')->default('default_photo_profile.jpg'); to $table->string('photo_url');
Hope you get it.