My project is, after a user come registered redirect to verification page.
public function register(Request $request)
{
Session::put('mobile',$mobile)
$code = rand(10000,99999);
$user = User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'gender' => $request->gender,
'mobile' => $request->mobile,
//continue code
}
return redirect()->route('verification/?session'.$session);
}
To get the value of Session use
Session::get('mobile')
anywhere in the controller or in blade file or anywhere where Session::class accessible.
Related
I am using Laravel Framework, I have generated all Register and Login through "$php artisan make:auth" command, now I have added a new column called "avatar" in the users table, and I want to set it to "noimage.jpg", so each time I register by default "noimage.jpg" will be added.
RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'avatar' => 'noimage.jpg' //How it suppose to be?
]);
}
You also have to add avatar to the $fillable property of your model. Otherwise you cannot assign it with create. See docs on Mass Assignment.
Instead you could manually assign the avatar:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->avatar = 'noimage.jpg';
return $user;
}
Another way to set a default value for your model is to use Laravel lifecycle:
const DEFAULT_AVATAR = 'noimage.jpg'
protected static function boot()
{
parent::boot();
static::creating(function (User $user) {
if (!$user->avatar) {
$user->avatar = self::DEFAULT_AVATAR;
}
});
}
See: https://laravel.com/docs/5.8/eloquent#events
My route/api.php has these routes:
Route::post('/signup' , 'UserApiController#signup');
Route::post('/logout' , 'UserApiController#logout');
Route::post('/verify' , 'UserApiController#verify');
but when I'm trying to access from Postman like this, it shows object not found:
localhost/my_webiste/api/signup
here the userapicontroller signup function:
public function signup(Request $request)
{
$this->validate($request, [
'social_unique_id' => ['required_if:login_by,facebook,google','unique:users'],
'device_type' => 'required|in:android,ios',
'device_token' => 'required',
'device_id' => 'required',
'login_by' => 'required|in:manual,facebook,google',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'mobile' => 'required',
'password' => 'required|min:6',
]);
try{
$User = $request->all();
$User['payment_mode'] = 'CASH';
$User['password'] = bcrypt($request->password);
$User = User::create($User);
return $User;
} catch (Exception $e) {
return response()->json(['error' => trans('api.something_went_wrong')], 500);
}
}
here the postman output of post request of localhost/mywebsite/api/signup :
<title>Object not found!</title>
<link rev="made" href="mailto:postmaster#localhost" />
<h1>Object not found!</h1>
The requested URL was not found on this server.
If you entered the URL manually please check your
spelling and try again.
Make sure, in your postman, to add the header accept = application/json.
Your code is correct. It seems like you are missing public from url
localhost/my_webiste/public/api/signup
I have been trying to get this api up and running and keep on experiencing this error when I test in Postman.
1. api.php
Route::group(['middleware' => ['api','cors']], function () {
Route::post('auth/register', 'Auth\RegisterController#create');
});
2. RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}}
Postman configuration set to Post, the body is set to raw and JSON(application/json) Below is the postman json code.
{
"name": "Walter White",
"email": "wwhite#someemaildomain.net",
"password": "testpassword"
}
Below is the error
Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed and exactly 1 expected in file C:\xampp\examplestuff
In order to fix your registration you should change your route definition to:
Route::group(['middleware' => ['api','cors']], function () {
Route::post('auth/register', 'Auth\RegisterController#register');
});
I assume your RegisterController is using the trait RegistersUsers. This trait is providing the register method, which is using the RegisterController::create method to create the new user itself.
Pass Request to your create function like:
protected function create(Request $request){...
and access your data like this:
$request->name
you have to do validation also plz check below answer
protected function create(Request $request)
{
$data = $request->json()->all();
$validator = Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $message){
$messages[] = $message[0];
}
$messages = $messages;
$message = implode(',',$messages);
$response = $messages;
return $response;
}else{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
I am developing a app in Laravel 5.3 where multiple users can login and create new users but restriction in view. The app will also have a main admin who will control overall app. Example:
Admin
User
2.1 Child User 1
2.2 Child User 2
To create roles and permission. You must refer to Entrust which is a well-known package which I find suitable for this task
you can do this like super admin can create user if he wants like
public function postCompanyRegistrationByAdmin (Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|unique:users',
'password' => 'required',
'company_name' => 'required',
'company_phone_no' => 'required',
'company_address' => 'required',
'company_city' => 'required',
'country' => 'required',
]);
$company = new Company;
$company->id = $user->id;
$company->name = $request->get("company_name");
$company->phone_no = $request->get("company_phone_no");
$company->address =$request->get("company_address");
$company->city = $request->get("company_city");
$company->company_logo = $request->get("company_logo");
$company->country = $request->get("country");
$company->save();
$message = 'Company successfully Registered.';
return redirect()->route('Companies')->with(['message' => $message]);
}
its route will be in middle where admin like
Route::group(array('namespace' => 'ShoesPlanner', 'middleware' => 'Admin'), function() {
Route::post('companyRegistrationByAdmin', array('as' => 'companyRegistrationByAdmin', 'uses' => 'SuperAdminController#postCompanyRegistrationByAdmin'));
});
Middleware will like
public function handle($request, Closure $next)
{
if (!Auth::guest() && Auth::user()->user_type == 'Admin') {
return $next ($request);
}
return redirect()->route('login');
}
I need to send an email after a new user is created.
But I don't know how to return to the home page without getting an error.
This is what I am doing right now.
User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
if (Auth::attempt(['email' => $data['email'], 'password' => bcrypt($data['password']) ])) {
// Authentication passed...
return redirect('/');
}
I keep getting this as my error message.
SErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/sniddl/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 63 and defined
Edit:
changed the title to reflect the answer.
Here is a modified create function with an added email function for your register controller
Make sure Request is included in the pages top with namespaces being used:
use Illuminate\Http\Request;
Change the create function in your controller:
protected function create(Request $data)
{
$user = new User;
$user->name = $data->input('name');
$user->username = $data->input('username');
$user->email = $data->input('email');
$user->password = bcrypt($data->input('password'));
$user->phone = bcrypt($data->input('phone'));
$user->confirmation_code = str_random(60);
$user->save();
if ($user->save()) {
$this->sendEmail($user);
return redirect('VIEWPATH.VIEWFILE')->with('status', 'Successfully created user.');
} else {
return redirect('VIEWPATH.VIEWFILE')->with('status', 'User not created.');
}
}
Create the sendEmail function in the same controller that will use Laravels built in email. Make sure you create and your HTML email:
public function sendEmail(User $user)
{
$data = array(
'name' => $user->name,
'code' => $user->confirmation_code,
);
\Mail::queue('EMAILVIEWPATH.HTMLEMAILVIEWFILE', $data, function($message) use ($user) {
$message->subject( 'Subject line Here' );
$message->to($user->email);
});
}
NOTE:
Your going to need to update the VIEWPATH.VIEWFILE and EMAILVIEWPATH.HTMLEMAILVIEWFILE at a minimium in the examples above.
Check the repos below for CONTROLLER :
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php
https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/Auth/AuthController.php
REGISTER VIEW(blade) EXAMPLE
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/auth/register.blade.php
EMAIL VIEW THAT RECEIVES VARIABLES:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php
Ok so it turns out, by setting User::create as a variable it allows you to login in the user by returning the variable. Like this.
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
return $user;