1

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?

2
  • what will be error you are getting dude? Commented Mar 4, 2020 at 14:41
  • I don't have an error... I don't know how to send the query to the view for make my select Commented Mar 4, 2020 at 14:43

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

9 Comments

thanks! this works with another pages... but I need the name of the route view from register in laravel.. do you know which is? instead of index what should I put? I'm working in the view of register in laravel
You can use view("register")->with(["generos" => $generos]);
where do I have to put this function? in my function validator? or I have to create another function to put on it
What is the name of your view where you want to send the variable?
I'm working on the view of register in laravel... so I didn't create the view... the view is in the framework
|
0

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!

2 Comments

I got an error with this............. syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) (View: C:\laragon\www\bduproject\resources\views\auth\register.blade.php)
I modified the "@enforeach" for "@endforeach".. because but nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.