简体   繁体   English

使用 laravel 中的 controller 将数据从数据库传递到刀片视图

[英]Passing data from the database to a blade view using a controller in laravel

I found several ways to do what I need to do, the first way was using the rounting using the web.php file.我找到了几种方法来做我需要做的事情,第一种方法是使用 web.php 文件使用路由。 But according to serveral posts this creates vulnerabilities in the application.但根据一些帖子,这会在应用程序中产生漏洞。 So I found out the correct usage is with a controller that manages the database queries.所以我发现正确的用法是使用管理数据库查询的 controller。

I created a controller, named EventsController and put this into it:我创建了一个名为 EventsController 的 controller 并将其放入其中:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EventsController extends Controller
{
    public function index()
    {
        $events = DB::table('eventaries')->select('id','coursname','start', 'end', 'category')->get();

        return view('components.course-list')->with('eventaries', $events);
    }
}

the blade is inside the folder: /resources/views/components/course-list.blade.php刀片位于文件夹内: /resources/views/components/course-list.blade.php

Inside the blade I use this code:在刀片内部,我使用以下代码:

<div class="px-6 py-20">
    <div class="max-w-7xl mx-auto">
        <!-- Course List -->

        {{ $events->coursname}}


    </div>
</div>

But I get the error:但我得到了错误:

Undefined variable $events (View: D:\laragon\www\censored\resources\views\components\course-list.blade.php)

->with('eventaries', $events) means that you are passing the value of $events as eventaries. ->with('eventaries', $events) 表示您将 $events 的值作为事件传递。 So in the blade you need to access it using $eventaries instead.因此,在刀片中,您需要使用 $eventaries 来访问它。 So now blade code would be:所以现在刀片代码将是:

    <div class="px-6 py-20">
    <div class="max-w-7xl mx-auto">
        <!-- Course List -->

        {{ $eventaries->coursname}}


    </div>
</div>

You have given 'eventaries' as the name for events.您已将“eventaries”作为事件的名称。 So you can only access it with $eventaries within the view, not as events.因此,您只能在视图中使用 $eventaries 访问它,而不是作为事件访问它。

you are passing to the view the value using the "with" method, and it does it like a value/key pair (->with($key, $value)).您正在使用“with”方法将值传递给视图,它就像一个值/键对 (->with($key, $value))。 In your case you declare it like在你的情况下,你声明它像

return view('components.course-list')->with('eventaries', $events);

so, in the view you can access the value through the $eventaries, not the $events.因此,在视图中,您可以通过 $eventaries 而不是 $events 访问该值。 Also, the query result is a collection and you will need to loop it to get each item此外,查询结果是一个集合,您需要循环它以获取每个项目

<div class="px-6 py-20">
    <div class="max-w-7xl mx-auto">
        <!-- Course List -->
        @foreach($eventaries as $event)
        {{ $event->coursename }}
        @endforeach
    </div>
</div>

When using the with method on a view the first argument (key) becomes the name of the variable in the view and the second argument (value) becomes it's value.当在视图上使用with方法时,第一个参数(键)成为视图中变量的名称,第二个参数(值)成为它的值。

This means you need to use $eventaries in your view instead of $events or rename the key in your controller return view('components.course-list')->with('events', $events);这意味着您需要在视图中使用$eventaries而不是$events或重命名 controller return view('components.course-list')->with('events', $events);的键。 . .

Also, I'm not sure about defining the action directly in the routes file causes vulnerabilities.此外,我不确定直接在路由文件中定义操作会导致漏洞。 I just think that the routes file, which is often the first entry point for developers when exploring a Laravel app, becomes hard to read/manage.我只是认为路由文件(通常是开发人员在探索 Laravel 应用程序时的第一个入口点)变得难以阅读/管理。

What caused the issue?是什么导致了这个问题?
The issue of this question was created because the router was never accessing the controller I created.创建此问题的问题是因为路由器从未访问我创建的 controller。 I added the following code to my router to access the controller:我将以下代码添加到我的路由器以访问 controller:

Route::get('/kursangebote/{course}', ['App\Http\Controllers\EventaryController', 'index'])->name('course.list-list');

In there I added the logic I wanted.在那里我添加了我想要的逻辑。

['App\Http\Controllers\EventaryController', 'index']

App\Http\Controllers\EventaryController is the controller I use, and index the function inside the controller I need to call. App\Http\Controllers\EventaryController是我使用的 controller,并index controller 里面的 function 我需要调用。 It might be a better Idea to use Eloquent for this https://laravel.com/docs/8.x/eloquent将 Eloquent 用于此https://laravel.com/docs/8.x/eloquent可能是一个更好的主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM