Test MySQL Connection In Laravel.

Hey, for a while now I have been breaking in my head how I can display a table from my database through the laravel controller, view and blade files. This is how you do it.

In this example,  I will be showing the content in a MySQL table called tareas with the following columns:

CREATE TABLE `tareas` (
  `title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `done` int DEFAULT NULL,
  `category_id` int DEFAULT NULL,
  `user_id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Here is some example data just incase you need it.

INSERT INTO `tareas` (`title`, `description`, `done`, `category_id`, `user_id`) VALUES
('djh658byXn', 'Exercitation cillum adipisicing qui laborum fugiat. Dolore dolore est minim excepteur dolore consequat irure anim. Anim reprehenderit esse amet velit. In ut ipsum occaecat velit.', 1, NULL, NULL),
('vlZr7Fh3lA', 'Exercitation cillum adipisicing qui laborum fugiat. Dolore dolore est minim excepteur dolore consequat irure anim. Anim reprehenderit esse amet velit. In ut ipsum occaecat velit.', 1, NULL, NULL),
('pdSPdhOAqd', 'Exercitation cillum adipisicing qui laborum fugiat. Dolore dolore est minim excepteur dolore consequat irure anim. Anim reprehenderit esse amet velit. In ut ipsum occaecat velit.', 1, NULL, NULL),

create new view blade and controller with these commands:


touch resources/views/tareaslist.blade.php

echo "<h1>Tareas List</h1>" > resources/views/tareaslist.blade.php

code resources/views/tareaslist.blade.php

php artisan make:controller TareaslistController

code app/Http/Controllers/TareaslistController.php


ADD show() FUNCTION TO TareaslistController CONTROLLER:

    function show(){
        view('tareaslist');
    }

Add routing to web.php

printf "\nRoute::get('/list',[App\Http\Controllers\TareaslistController::class, 'show']);" >> routes/web.php

code  routes/web.php

in web.php:
ADD: use App\Http\Controllers\TareaslistController;
AFTER: use Illuminate\Support\Facades\Route;

TRY IT: http://localhost/list


CREATE A MODEL FOR THE DATABASE:
IN THIS EXAMPLE, THE TABLE NAME IS tareas SO THE MODEL NAME SHOULD BE tarea WITHOUT THE S AND CAPITALIZED


php artisan make:model Tarea


code app/Models/Tarea.php


IN TareaslistController.php
ADD: use App\Models\Tarea;
AFTER: use Illuminate\Http\Request;

IN TareaslistController.php
COMMENT: //return view('tareaslist');
ADD:  return Tarea::all();

TRY IT: http://localhost/list

NOW LETS PASS THE OBJECT TO THE VIEW BLADE TEMPLATE

IN TareaslistController.php CHANGE THE show() FUNCTION TO:


    function show(){
        //
        $data = Tarea::all();
        return view('tareaslist', ['tareas'=>$data] );
    }



Open tareaslist.blade.php add the following HTML:

<h1>Tareas Lists</h1>

<table border="1">
<tr>
<td>title</td>
<td>description</td>
<td>done</td>
<td>category_id</td>
<td>user_id</td>
</tr>
@foreach($tareas as $tarea)
<tr>
    <td>{{$tarea['title']}}</td>
    <td>{{$tarea['tidescriptiontle']}}</td>
    <td>{{$tarea['done']}}</td>
    <td>{{$tarea['category_id']}}</td>
    <td>{{$tarea['user_id']}}</td>
</tr>
@endforeach
</table>


TRY IT: http://localhost/list