I am having trouble in laravel 5.1 registration in the AuthController the validate and create method. I make may login and register forms in one page so I added register- on the inputs of the registration form (ex. register-email for the email address in the registration form). So my validate method in the AuthController is this:
App\Auth\Http\Controllers\Auth
/**
* 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, [
'last-name' => 'required|between:2,35',
'first-name' => 'required|between:2,35',
'register-email' => 'required|email|max:255|unique:users',
'register-password' => 'required|confirmed|min:8',
'register-confirm-password' => 'same:register-password'
]);
}
The last-name, first-name, register-email, etc is a html inputs right? So I think this is right?
and then this is the create method:
protected function create(array $data)
{
return User::create([
'last-name' => $data['last-name'],
'first-name' => $data['first-name'],
'email' => $data['register-email'],
'password' => bcrypt($data['register-password'])
]);
}
The keys in the create method is the database table right? and the value is the html input name/id? I am getting Unknown column error of register-email why is that? I know I don't have register-email column in the database but, in the create method I specify the email column in the database and not register-column which is the html input name of my registration form. Please help.
BTW this is my database migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('last-name');
$table->string('first-name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
Related
In my database i have this
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->string('admin')->nullable();
$table->string('usertype')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Which means in my form i have this
<select name="usertype">
<option>Select Usertype</option>
<option value="1">Landlord</option>
<option value="2">Agent</option>
<option value="3">Tenant</option>
</select>
Users can select the type of user they are.
I have this in my RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'username' => $data['username'],
'usertype' => $data['usertype'],
'password' => Hash::make($data['password']),
]);
}
When a user which is landlord registers it takes them to http://127.0.0.1:8000/dashboard
How can i make the content different for each users i.e
1. I want landlord to be able to just add property
2. I want tenant to be able to edit profile, see properties they have bought.
3. I want agent to able to add property like landlord.
How can i achieve this ?
You can make use of Laravel Policies. Example if you have a PropertyController, you can have a PropertyPolicy with the method:
/**
* Determine whether the user can create models.
*
* #param \App\User $user
* #return mixed
*/
public function create(User $user)
{
return $user->usertype == 'Landlord';
}
And in the view, you can do something like:
#can('create', $property)
// link to create property
#endcan
learn more about Laravel Policies at https://laravel.com/docs/7.x/authorization#creating-policies
I've set up an admin log in but upon submitting the form i'm thrown an error. I've tried globally searching restaurant_users but i'm unable to find anything.
login controller:
public function login(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('restaurantuser')->attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->intended(route('dish.create'));
}
return redirect()->back()->withInput($request->only('email'));
}
schema:
Schema::create('restaurantuser', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
error:
SQLSTATE[HY000]: General error: 1 no such table: restaurant_users (SQL: select * from "restaurant_users" where "email" = admin#test.com limit 1)
If any other code is needed let me know and ill provide it.
In your User model add this:
protected $table = 'restaurantuser':
The restaurant_users comes from Laravel as it assumes that if your model is called RestaurantUser that that's the table name.
Hello fellow webApp student.
Would you be kind enough to share how you implemented the administrator role, and the corresponding account approval if you did end up completing it.
Much appreciated,
someone close by...
In one of my Laravel 5.6 applications, I need to add a few more fields in the default registration form.
So I've added only one field to test [phone_no] in the default registration form registration.blade.php, and also add phone_no RegisterController, but when I click on the register button it shows the following errors:
"SQLSTATE[HY000]: General error: 1364 Field 'phone_no' doesn't have a default value (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Jahid Hossain, jahid.cse18#gmail.com, $2y$10$wYg6jAihCz.v09PUXngF/ekjNvZgxb4Rq4GI3i.glfzXt37oGnbSO, 2018-04-20 06:24:27, 2018-04-20 06:24:27))
So here is the migration file code:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('status')->default('active');
$table->string('created_by')->default('SYSTEM');
$table->string('modified_by');
$table->string('phone_no');
$table->rememberToken();
$table->timestamps();
});
}
And here is the code of RegisterController:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'created_by' => $data['email'],
'phone_no' => $data['phone_no'],
]);
}
So I think I am missing something. Can anyone tell me how can I add one/more fields in the Laravel 5.6 default registration form and save those field successfully...
Thanks
Please add your custom field in below way.
In your model add the your custom field in protected $fillable=['name', 'email', 'password', 'created_by', 'phone_no'] in your model
protected $fillable = [ 'name', 'email', 'password', 'created_by', 'phone_no', 'address'] // in your model
also add these field to your migration also
phone_no default means you are sending phone_no blank.
I'm building a web app with laravel.
First Question:
There's a simple form on users dashboard to fill and save.
Here's the model:
class Salon extends Model
{
protected $table = 'salons';
protected $fillable = [
'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'
];
public $timestamps = false;
}
and here is the controller :
public function store(Request $request)
{
$user_id = Auth::user()->id;
Salon::create([
'user_id' => $user_id,
'salonname' => $request['salonname'],
'saloncity' => $request['saloncity'],
'salonaddress' => $request['salonaddress'],
'salontel' => $request['salontel'],
'salonmob' => $request['salonmob'],
'salonsite' => $request['salonsite'],
'saloncat' => $request['saloncat'],
'salonkhadamat' => $request['salonkhadamat'],
'salonkhadamatprice' => $request['salonkhadamatprice'],
'salondesc' => $request['salondesc'],
'saloninsta' => $request['saloninsta'],
'salontelegram' => $request['salontelegram'],
'salontags' => $request['salontags']
]);
return 'done!';
}
And the routes:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/salons/add', function () {
return view('add_salon');
})->middleware('auth');
Route::post('salons', 'SalonsController#store');
Route::get('salons', function () {
return 'Hi';
});
When I complete the form and hit send button, it returns this error :
"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into salons (salonname,...
Where am I doing wrong?
I created a table migration as :
public function up()
{
Schema::create('Salons', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('salonname');
$table->string('saloncity');
$table->string('salonaddress');
$table->integer('salontel');
$table->integer('salonmob');
$table->string('salonsite');
$table->string('saloncat');
$table->string('salonkhadamat');
$table->integer('salonkhadamatprice');
$table->string('salondesc');
$table->string('saloninsta');
$table->string('salontelegram');
$table->string('salontags');
$table->timestamps();
});
}
user_id is using a foreign reference from users table.
let me explain the process, consider we have some users registered on our app, some of them want to add their salons on our website, so we want to use the user_id from the users table on salons table, so we can return salons with the user's data (profile) on our homepage.
Second question:
If a salon have two separate telephone numbers, How can I store them in this table separately? I mean, people can add many telephone-numbers as they want. Or as many addresses as they have, in separate fields.
Third question:
For creating a portfolio section for each salon, Should I create a new table such as attachments to have pictures addresses and salon id to return them on their respective page later?
Add user_id to the fillable array too:
protected $fillable = ['user_id', 'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'];
Or use the relationship if it is defined:
$salon = auth()->user()->salons()->create([....
If it's not defined:
public function salons()
{
return $this->hasMany(Salon::class);
}
We are having a strange issue with Laravel 5 in that it is refusing to store the checkbox value.
We are adapting the existing registration form that comes bundled with Laravel 5 and we are adding an optin checkbox but it seems the model does not recognise this as a field even though we are adding it as a field in the migration file.
Any help on this would be appreciated.
Mirgration File:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password', 60);
$table->date('dob');
$table->boolean('optin')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
Then we add it to the register.blade.php file:
<div class="form-group">
<label class="col-md-4 control-label">Optin</label>
<div class="col-md-6">
<input type="checkbox" class="form-control" name="optin">
</div>
</div>
At the point of creating the User model, we check the value of the checkbox and assign it.
protected function create(array $data)
{
//this does return 1 or 0 as expected
$optin = ($data["optin"] == "on") ? 1 : 0;
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'dob' => $data['dob'],
'optin' => $optin
]);
}
But at this point the field is null. No value is entered into the database...
Have you put the field 'optin' in the $fillable array within the model? Otherwise you cant create a User with 'optin' using the static create method.
//File: User.php
protected $fillable = ['optin'];
The model already has a static create() function. Therefore, when you make a call like User::create($data) from your controller, your function is not called.
My approach is to change the name of your function and make it static.
Update
Also you can override the create function:
public static function create(array $attributes)
{
$attributes["optin"] = ($attributes["optin"] == "on") ? 1 : 0;
return parent::create($attributes);
}