How to send a query in a variable using laravel? - laravel

I have this function to register my users
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'apellido' => ['required', 'string', 'max:255'],
'idop' => ['required', 'string', 'max:6', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'cedula' => ['required', 'int'],
'fecha_nacimiento' => ['required', 'date'],
'fecha_ingreso' => ['required', 'date'],
'extension' => ['required', 'int'],
'movil' => ['required', 'int'],
'tel_hab' => ['required', 'int']
]);
}
I would like to send this query in a variable ($generos) to make a select
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
How could I do that?

You can pass the variable from controller to view using below ways:
Way1
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", compact("generos "));
Way2
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", ["generos" => $generos]);
Way3
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index")->with(["generos" => $generos]);
On your view file::
<select name="genero">
<option value=''>select</option>
#foreach ($generos as $genero)
<option value="{{ $genero->id }}">{{ $genero->name}}</option>
#enforeach
</select>

by the comments, you want to send the result of the query to the view. So,
In your controller function do:
return view('generos.index', [
'generos' => DB::table('tbl_lista_generos')->select('id','genero')->get()
]);
In the view:
<select class='' name='select_form_generos'>
<option value=''>select genero</option>
#foreach ($generos as $genero)
<option value='{{ $genero->id }}'>{{ $genero->genero}}</option>
#enforeach
</select>
Hope it helped!

Related

Get a session element in the view with Laravel

I'm trying to implement Google Recaptcha in my user registration form.
Controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'surname' => ['required', 'string', 'max:255'],
'nickname' => ['required', 'string', 'max:255', 'min:5', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'g-recaptcha-response' => function ($attribute, $value, $fail) {
$secretKey = config('services.recaptcha.secret');
$response = $value;
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$response&remoteip=$userIP";
$response = file_get_contents($url);
$response = json_decode($response);
if (!$response->success) {
Session::flash('errorCaptcha', 'Per favore controlla il reCaptcha!');
$fail($attribute . 'Google reCaptcha fallito!');
}
}
]);
}
Instead, the code in the view should show an error message if the user does not confirm the Recaptcha.
Blade
#if(Session::has('errorCaptcha'))
<span class="invalid-feedback" role="alert">
<strong>{{ Session::get('errorCaptcha') }}</strong>
</span>
#endif
In the controller I run:
dd(session::get('errorCaptcha'))
I get the value in the controller but not in the view. Can anyone help me, please?

Avoid html tags in laravel string validation

I want to prevent users of using html tags in their username fields when they are registering, how can I do that?
For instance currently I have a user he registered with such username
E7JfyqxE4lsbqQ <html><img src="https://...../d28/2011/19/93045d3fb9c4.jpg" width="600" height="234" alt="bill"> </html>
I just want to allow letters and numbers (a-z, 0-9) that's all.
RegisterController
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => 'required|string|max:255|unique:users|not_exist',
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
If you only want to allow alphanumeric characters, you can use Laravel's built in alpha_num validation.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'alpha_num', 'max:255'],
'username' => 'required|string|alpha_num|max:255|unique:users|not_exist',
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
You can do something like this
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => [
'required',
'string',
'max:255',
'string',
'unique:users',
'not_exist',
'not_regex:<\s*a[^>]*>(.*?)<\s*/\s*a>'
],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
You can add more regex if you have more to include

laravel select box value not adding to database

so am trying to send the value of a select box to database to calculate admin roles but it doesn't receive the value here is my view
<div class="form-group row">
<label for="exampleFormControlSelect1" class="col-md-4 col-form-label text-md-right">Role</label>
<div class="col-md-6">
<select class="form-control" id="exampleFormControlSelect1" name="role">
<option value="1">Super Admin</option>
<option value="2">Admin</option>
<option value="3">Doctor</option>
<option value="4">Sales</option>
</select>
</div>
</div>
and this is my controller its for adding new admins to the database (not users with admin roles)
public function showRegistrationForm()
{
return view('auth.admin-register');
}
public function register(Request $request)
{
// Validate form data
$this->validate($request,
[
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:admins'],
'password' => ['required', 'string', 'min:8'],
'role' => ['required']
]
);
dd($request->role);
// Create admin user
try {
$admin = Admin::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'role' => $request->role
]);
return redirect()->route('admin.dashboard');
} catch (\Exception $e) {
return redirect()->back()->withInput($request->only('name', 'email'));
}
}
the dd($request->role) works and return the nvalue of the but the problem is with the
try {
$admin = Admin::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'role' => $request->role
]);
In model you can define protected columns then all the other column will be auto fillable
or you can define fillable columns in model
The problem was that I didn't set the role in fillable in admin model

validateUsername does not exist

when i am tried to add one text field which is called username for registration in laravel it says "Method Illuminate\Validation\Validator::validateUsername does not exist.
i am trying to be change the datatype of the column but still does not give any other response
{{ __('username') }}
#error('username')
{{ $message }}
#enderror
You are calling the validator method creator on a type that it doesn't know anything about. I'm guessing that you took something like:
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'],
]);
}
and copied the email line and replaced 'email' with 'username' in two places, yielding something like this:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'username', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
the problem is that the second 'email' isn't doing the same thing that the first one is. The first 'email' is your field name. The second one, however, is a validation type to ensure that the value is actually a valid email (e.g., user#mail.com). The problem, then, is that the validator doesn't know anything about a username validation type, and so when it tries to construct that method, it fails.
What you need to do is just get rid of your second reference to username, yielding this:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
Please post your validation rules. This can either you controller code (i.e. from $this->validate or your form request class.
Alternatively, if you're using the default Laravel auth scaffolding, please post the contents of your registration controller (RegisterController).
Finally, do rule out the obvious (sorry), does your database definitly have a column in the users table titled username? For example, have you followed a guiode such as http://techiescircle.com/add-extra-fields-laravel-5-registration-form/ to add a new field to the users table?
It appears you're incorrectly attempting to run a validation rule that doesn't yet exist.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
This is the issue
Right
'username' => ['required', 'string', 'max:255', 'unique:users'],
wrong
'username' => ['required', 'string','username', 'max:255', 'unique:users'],

How do I customize the Laravel registration form error message?

protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users'],
'phone' => ['required', 'unique:users'],
'password' => ['required', 'string', 'min:6']
]);
}
I am sending the member form via Ajax. I want to show a custom message when I receive an error message.
public function messages()
{
return [
'first_name.required' => 'A title is required'
];
}
I tried the functions of these messages but it didn't work. Is there another way? Thanks. My English is bad, sorry.
You can pass the messages array as 3rd parameter in Validator::make method. Like following:
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users'],
'phone' => ['required', 'unique:users'],
'password' => ['required', 'string', 'min:6']
], [
'first_name.required' => 'A title is required'
]
);
}

Resources