英文:
how to show dropdown list in multiple view blade using laravel
问题
现在我想要在多个页面上显示这个下拉列表,比如 Login.blade 和 register.blade。
控制器(Controller):
class DropDownController extends Controller
{
public function dropdownlist()
{
$rolls['roll'] = Roll::get(['roll_id', 'roll']);
return view('auth.register', $rolls);
// return view('auth.login', $rolls);
}
}
路由(Route):
Route::get('register', [DropDownController::class, 'dropdownlist']);
登录 Blade 模板:
<div class="form-group">
<label>用户</label>
<select name="roll">
<option value="" selected disabled>选择用户</option>
@foreach($roll as $row)
<option value="{{$row->roll_id}}">{{$row->roll}}</option>
@endforeach
</select>
<span class="text-danger">
@error('roll')
{{ $message }}
@enderror
</span>
</div>
英文:
I have created a dropdown list using Laravel. Now I want this list to show on multiple pages. Like a Login.blade and register.blade.
Controller:
class DropDownController extends Controller
{
public function dropdownlist()
{
$rolls['roll'] = roll::get(["roll_id", "roll"]);
return view('auth.register', $rolls);
// return view('auth.login', $rolls);
}
}
Route:
Route::get('register',[DropDownController::class,'dropdownlist']);
Login Blade:
<div class="form-group">
<label>Users</label>
<select name="roll">
<option value="" selected disabled>Select Users</option>
@foreach($roll as $row)
<option value="{{$row->roll_id}}">{{$row->roll}}</option>
@endforeach
</select>
<span class="text-danger">
@error('roll')
{{ $message }}
@enderror
</span>
</div>
Now I want this list to show on multiple pages. Like a Login.blade and register.blade.
答案1
得分: 0
要在Laravel中的多个视图中显示下拉列表,您可以像这样使用每个函数:
$rolls = Roll::select('roll_id', 'roll')->get();
return view('auth.register', compact('rolls'));
还可以在登录控制器中使用:
$rolls = Roll::select('roll_id', 'roll')->get();
return view('auth.login', compact('rolls'));
现在可以在页面中使用这个变量,并且如果您尝试这样做,可以在用户模型中创建全局函数,然后在任何地方使用它。
<div class="form-group">
<label>Users</label>
<select name="roll">
<option value="" selected disabled>Select Users</option>
@foreach($rolls as $row)
<option value="{{$row->roll_id}}">{{$row->roll}}</option>
@endforeach
</select>
<span class="text-danger">
@error('rolls')
{{ $message }}
@enderror
</span>
</div>
请注意,上述代码中的Roll
是一个模型名称,您需要根据实际模型名称进行替换。
英文:
To show a dropdown list in multiple view blades using Laravel, you can use every function like that-
$rolls=roll::select('roll_id','roll')->get();
return view('auth.register', $rolls);
and also use in login controller-
$rolls=roll::select('roll_id','roll')->get();
return view('auth.login', $rolls);
now use this variable then show in pages
and if you try this then create global function in user model and use anywhere
<div class="form-group">
<label>Users</label>
<select name="roll">
<option value="" selected disabled>Select Users</option>
@foreach($rolls as $row)
<option value="{{$row->roll_id}}">{{$row->roll}}</option>
@endforeach
</select>
<span class="text-danger">
@error('rolls')
{{ $message }}
@enderror
</span>
</div>
答案2
得分: 0
如果我理解正确,最好的方法是将下拉菜单创建为一个组件,并使用视图组件(view composer)将所需的变量传递给它。
有关视图组件的一些文档:
https://laravel.com/docs/10.x/views#view-composers
在需要下拉菜单的页面上,你可以使用@include('dropdown-menu')
或<x-dropdown-menu />
。
使用视图组件将允许你将变量传递到你定义的组件视图中。
英文:
If i'm understanding correctly you would be best to create your dropdown as a component and using a view composer to pass the required variables into it.
Some documentation of view composers:
https://laravel.com/docs/10.x/views#view-composers
within the pages you require the dropdown you can either use @include('dropdown-menu')
or <x-dropdown-menu />
Using the view composer will allow you to pass the variables into the component view that you define.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论