I found several ways to do what I need to do, the first way was using the rounting using the web.php file. 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.
I created a controller, named EventsController and put this into it:
<?php namespace AppHttpControllers; use IlluminateHttpRequest; 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
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:laragonwwwcensoredresourcesviewscomponentscourse-list.blade.php)
Advertisement
Answer
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.
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);
.
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.