如何使用 laravel 在变量中发送查询?
How to send a query in a variable using laravel?
我有这个功能来注册我的用户
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']
]);
}
我想在变量 ($generos) 中发送此查询以生成 select
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
我该怎么做?
根据评论,您想将查询结果发送到视图。所以,
在你的控制器函数中做:
return view('generos.index', [
'generos' => DB::table('tbl_lista_generos')->select('id','genero')->get()
]);
在视图中:
<select class='' name='select_form_generos'>
<option value=''>select genero</option>
@foreach ($generos as $genero)
<option value='{{ $genero->id }}'>{{ $genero->genero}}</option>
@enforeach
</select>
希望对您有所帮助!
您可以使用以下方式将变量从 controller
传递到 view
:
方式 1
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", compact("generos "));
方式2
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", ["generos" => $generos]);
方式 3
$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>
我有这个功能来注册我的用户
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']
]);
}
我想在变量 ($generos) 中发送此查询以生成 select
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
我该怎么做?
根据评论,您想将查询结果发送到视图。所以,
在你的控制器函数中做:
return view('generos.index', [
'generos' => DB::table('tbl_lista_generos')->select('id','genero')->get()
]);
在视图中:
<select class='' name='select_form_generos'>
<option value=''>select genero</option>
@foreach ($generos as $genero)
<option value='{{ $genero->id }}'>{{ $genero->genero}}</option>
@enforeach
</select>
希望对您有所帮助!
您可以使用以下方式将变量从 controller
传递到 view
:
方式 1
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", compact("generos "));
方式2
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", ["generos" => $generos]);
方式 3
$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>