如何在Laravel中的多个视图Blade中显示下拉列表?

huangapple go评论66阅读模式
英文:

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[&#39;roll&#39;] = roll::get([&quot;roll_id&quot;, &quot;roll&quot;]);
        return view(&#39;auth.register&#39;, $rolls);   
        // return view(&#39;auth.login&#39;, $rolls);
    }
   
}

Route:

Route::get(&#39;register&#39;,[DropDownController::class,&#39;dropdownlist&#39;]);

Login Blade:

&lt;div class=&quot;form-group&quot;&gt;
                    &lt;label&gt;Users&lt;/label&gt;
                    &lt;select name=&quot;roll&quot;&gt;
                        &lt;option value=&quot;&quot; selected disabled&gt;Select Users&lt;/option&gt;
                        @foreach($roll as $row)
                        &lt;option value=&quot;{{$row-&gt;roll_id}}&quot;&gt;{{$row-&gt;roll}}&lt;/option&gt;
                        @endforeach
                    &lt;/select&gt;
                    &lt;span class=&quot;text-danger&quot;&gt;
                        @error(&#39;roll&#39;)
                        {{ $message }}
                        @enderror
                    &lt;/span&gt;
                &lt;/div&gt;

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(&#39;roll_id&#39;,&#39;roll&#39;)-&gt;get();
return view(&#39;auth.register&#39;, $rolls); 

and also use in login controller-

$rolls=roll::select(&#39;roll_id&#39;,&#39;roll&#39;)-&gt;get();
return view(&#39;auth.login&#39;, $rolls); 

now use this variable then show in pages
and if you try this then create global function in user model and use anywhere

&lt;div class=&quot;form-group&quot;&gt;
                    &lt;label&gt;Users&lt;/label&gt;
                    &lt;select name=&quot;roll&quot;&gt;
                        &lt;option value=&quot;&quot; selected disabled&gt;Select Users&lt;/option&gt;
                        @foreach($rolls as $row)
                        &lt;option value=&quot;{{$row-&gt;roll_id}}&quot;&gt;{{$row-&gt;roll}}&lt;/option&gt;
                        @endforeach
                    &lt;/select&gt;
                    &lt;span class=&quot;text-danger&quot;&gt;
                        @error(&#39;rolls&#39;)
                        {{ $message }}
                        @enderror
                    &lt;/span&gt;
                &lt;/div&gt;

答案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(&#39;dropdown-menu&#39;) or &lt;x-dropdown-menu /&gt;

Using the view composer will allow you to pass the variables into the component view that you define.

huangapple
  • 本文由 发表于 2023年6月19日 13:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定