从Web路线中获取数据库-Laravel

发布于 01-19 11:54 字数 1117 浏览 3 评论 0原文

我有这个 Laravel 应用程序,我想在其中显示页面中登录用户的名称。事实是,我正在 web.php 文件中获取数据,如下所示:

Route::get('/', function () {
    $welcomeNames = DB::table('users')->get();
    return view('welcome', ['welcomeNames' => $welcomeNames]);
});

然后,欢迎您。 Blade.php,我返回这样的数据:

@if (!Auth::guest())

        @foreach($welcomeNames as $key => $data)
            @if ($data->name == Auth::user()->name )
                <tr>    
                <th>{{$data->name}}</th>             
                </tr>
            @endif
        @endforeach
                
        <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();" class="text-sm text-gray-700 dark:text-gray-500 underline">
            Logout
        </a>    
        <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
            {{ csrf_field() }}
        </form> 
        
    @else
                        
@endif

我的问题是:这是一种安全的做法吗?我不知道从我的路线中的数据库获取数据是否安全。提前致谢!

I have this Laravel application, in which I want to display the name of the logged in user in the pages.. Thing is, I'm getting the data in the web.php file, like this:

Route::get('/', function () {
    $welcomeNames = DB::table('users')->get();
    return view('welcome', ['welcomeNames' => $welcomeNames]);
});

And then, in my welcome.blade.php, I return the data like this:

@if (!Auth::guest())

        @foreach($welcomeNames as $key => $data)
            @if ($data->name == Auth::user()->name )
                <tr>    
                <th>{{$data->name}}</th>             
                </tr>
            @endif
        @endforeach
                
        <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();" class="text-sm text-gray-700 dark:text-gray-500 underline">
            Logout
        </a>    
        <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
            {{ csrf_field() }}
        </form> 
        
    @else
                        
@endif

My question is: is this a safe practice? I don't know if getting the data from the DB in my route is safe. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅2025-01-26 11:54:53

只要您不将其显示在不属于的地方,它就安全。但是它的不良习惯,如果不需要的话,不应该做。控制器以任何方式都更加有条理。

It is safe as long as you do not show it somewhere where it doesn't belong. But its bad practice and shouldn't be done if not really needed. AND controllers are way more organised in any way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文