Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Matrix166's avatar

Steps to display data from an external database

Hello there! I'm just another Laravel beginner who is looking for a suitable CMS/Framework for his workprojects.

I'm trying to wrap my head around the MVC workflow, but I'm currently struggling. I've been following Laravel tutorials from Lynda, especially the one with building a hotel room booking system, if anyone is familiar with that one.

However, that tutorial focuses on a database created with the CMD/terminal. I want to use an external, already existing database which I can control from PHPMyAdmin.

Goal here is to create CRUD-methods for meat/fish products. Let's keep it simple now, I just have two columns, 'name' and 'latin_name'. And for now, I only want to display data.

So, my questions:

  1. What are the optimal steps for making a raw query, to displaying the results in a HTML table?
  2. Should the controller or model handle the database queries?
  3. Do I need to create a fish/meat object and give it the correct values, when all I do is just get the query result and display it?
  4. Is my way of doing things completely "killing" the effectiveness of Laravel?

I'm not necessarily looking for a complete code, but I don't reject that option if someone wants to do that.

Thank you already for even taking a look at my question!

0 likes
1 reply
MorganRowse's avatar

Take a peak at the doc concerning Resource Controllers

https://laravel.com/docs/5.5/controllers#resource-controllers

Make sure your database is connected and setup in the .env file

Make sure a table like below exists and has some entries (keep using phpMyAdmin for this now, Look at migrations in the future):

products

  • id
  • name
  • latin_name

Run the below in a terminal for the C in MVC

$ php artisan make:controller ProductController --resource

Find the created ProductController.php under app\Http\Controllers and edit the index function like so:

//make sure you include an import for the App\Product model class

public function index()
    {
    $products = Product::get();

        return view('products.index')->with(compact($products));
    }

Then add the line to your routing routes\web.php file

Route::resource('products', 'ProductController');

Run the below in a terminal for the M in MVC

$ php artisan make:model Product

Now make the View for the V in MVC by creating a file under resources\views\products (you need to make the products folder) called index.blade.php

<table>
@foreach($products as $product)
    <tr><td>{{$product->name}} Latin:{{$product->latin_name}}</td></tr>
@endforeach 
</table>

Now use a browser to browse to your Laravel Application like:

localhost/products

You often don't need to make 'raw queries' when using an ORM like Eloquent.

The Model is an abstraction to the database, the Controller is a store for the business logic which interacts with the Model which interacts with the database.

You should make a products database table and a Product model with a flag to say if its a fish or meat product.

It can seem daunting at first, but Laravel is a powerful tool that just needs some prior knowledge.

Good luck!

Please or to participate in this conversation.