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

konrms's avatar

Display query results in table form

Hi guys!

I have created a form which accepts a user number input and after submitting it, the user receives some personal data from a local oracle db (which made it to connect to laravel thanks to you!). The query runs fine, but the results appear in list (I use the dd($entries) at the 3rd excerpt). How can I edit my code to appear in a simple table format (simple cells in one line)?

My code excerpts are as below:

  1. routes.php (or web.php for newer laravel version)
<?php
Route::get('/search', 'PagesController@search'); //find student data!
Route::post('/dedomena', 'NewController@psaxe');
  1. search.blade.php (it's the completion form)
<!DOCTYPE html>
<html lang="en">
<head>


    <meta charset="UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="form_style.css">  -->
    <title>Εισαγωγή Δεδομένων Χρήστη</title>
</head>
<body>

Εισαγωγή Δεδομένων

<form method="post" action="/dedomena" id="forma">

  {{csrf_field()}}

    <fieldset>
        <legend>Εισαγωγή</legend>

        Αριθμός Μητρώου:<br>
        <input type="number" name="AM" min="1000000" max="9999999"><br>
        <input type="submit" value="Αναζήτηση">
    </fieldset>
</form>

</body>
</html>
  1. And finally the NewController.php
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;

use DB;


class NewController extends Controller

    public function psaxe(Request $request)
    {

        $myVar = $request->get('AM');
        echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
        echo($myVar);
        $entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
        dd($entries); 

    }

}
0 likes
14 replies
konrms's avatar

@VILFAGO - Hi!

I just modified the controller file as follows (in fact I want somehow to pass $entries to pinakas.blade). Unfortunately it doesn't work. Could you help me a little with passing variable? please check last code line of my controller below). The final result is a table with 1 line and 49 columns:

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;

use DB;


class NewController extends Controller


{

    public function psaxe(Request $request)
    {

        $myVar = $request->get('AM');
        echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
        echo($myVar);
        $entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
               return view('pinakas', [$entries1 => $entries]);

    }


}
  1. This is the pinakas.blade where I want data to be inserted:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Αποτελέσματα</title>
</head>
<body>

<table border="1">
    <tr>
        <th>AM</th>
        <th>ST</th>
        <th>CS</th>
        <th>REGYEAR</th>
        <th>REGTERMIN</th>
        <th>CSYEAR</th>
        <th>REGTYPE</th>
        <th>REGDESCR</th>
        <th>STATUS</th>
        <th>STGROUP</th>
        <th>SEM1</th>
        <th>SEM2</th>
        <th>SEM3</th>
        <th>SEM4</th>
        <th>SEM5</th>
        <th>SEM6</th>
        <th>SEM7</th>
        <th>SEM8</th>
        <th>SEM9</th>
        <th>SEM10</th>
        <th>SEM11</th>
        <th>SEM12</th>
        <th>SEM1B</th>
        <th>SEM2B</th>
        <th>SEM3B</th>
        <th>SEM4B</th>
        <th>SEM5B</th>
        <th>SEM6B</th>
        <th>SEM7B</th>
        <th>SEM8B</th>
        <th>SEM9B</th>
        <th>SEM10B</th>
        <th>SEM11B</th>
        <th>SEM12B</th>
        <th>SEM1P</th>
        <th>SEM2P</th>
        <th>SEM3P</th>
        <th>SEM4P</th>
        <th>SEM5P</th>
        <th>SEM6P</th>
        <th>SEM7P</th>
        <th>SEM8P</th>
        <th>SEM9P</th>
        <th>SEM10P</th>
        <th>SEM11P</th>
        <th>SEM12P</th>
        <th>TOTAL</th>
        <th>TOTALB</th>
        <th>TOTALP</th>
    </tr>
    
    <tr>
        <td><?= $entries1?></td>

    </tr>
  
</table>

</body>
</html>
Vilfago's avatar

You were close :

 return view('pinakas', ['entries1' => $entries]);

But in your view, you will have everything in the first column, or an error if you get many instances.

@foreach($entries1 as $entry)
<tr>
  <td>{{ $entry->AM }}</td>
  <td>{{ $entry->ST }}</td>
  <td>{{ $entry->CS }}</td>
  <-- etc... -->
</tr>
@endforeach
1 like
konrms's avatar

That didn't work. I got the table empty...

konrms's avatar

@VILFAGO - That doesn't work too.

I changed the pinakas.blade.php as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Αποτελέσματα</title>
</head>
<body>
@php
    dd($entries1);
@endphp




</body>
</html>

But I get error:

Αποτελέσματα για τον φοιτητή με ΑΜ: 1030995 @php dd($entries1); @endphp 
Vilfago's avatar

it don't seem an error ... Which laravel version do you use that don't interpret @php ?

Vilfago's avatar
<!DOCTYPE html>
<html lang="el">
<head>
    <meta charset="UTF-8">
    <title>Αποτελέσματα</title>
</head>
<body>

<table border="1">
    <tr>
        <th>AM</th>
        <th>ST</th>
        <th>CS</th>
        <th>REGYEAR</th>
        <th>REGTERMIN</th>
        <th>CSYEAR</th>
        <th>REGTYPE</th>
        <th>REGDESCR</th>
        <th>STATUS</th>
        <th>STGROUP</th>
        <th>SEM1</th>
        <th>SEM2</th>
        <th>SEM3</th>
        <th>SEM4</th>
        <th>SEM5</th>
        <th>SEM6</th>
        <th>SEM7</th>
        <th>SEM8</th>
        <th>SEM9</th>
        <th>SEM10</th>
        <th>SEM11</th>
        <th>SEM12</th>
        <th>SEM1B</th>
        <th>SEM2B</th>
        <th>SEM3B</th>
        <th>SEM4B</th>
        <th>SEM5B</th>
        <th>SEM6B</th>
        <th>SEM7B</th>
        <th>SEM8B</th>
        <th>SEM9B</th>
        <th>SEM10B</th>
        <th>SEM11B</th>
        <th>SEM12B</th>
        <th>SEM1P</th>
        <th>SEM2P</th>
        <th>SEM3P</th>
        <th>SEM4P</th>
        <th>SEM5P</th>
        <th>SEM6P</th>
        <th>SEM7P</th>
        <th>SEM8P</th>
        <th>SEM9P</th>
        <th>SEM10P</th>
        <th>SEM11P</th>
        <th>SEM12P</th>
        <th>TOTAL</th>
        <th>TOTALB</th>
        <th>TOTALP</th>
    </tr>
@foreach($entries1 as $entry)
    <tr>
        <td>{{ $entry->AM }}</td>
        <td>{{ $entry->ST }}</td>
        <td>{{ $entry->CS }}</td>
        <td>{{ $entry->REGYEAR }}</td>
        <td>{{ $entry->REGTERMIN }}</td>
        <td>{{ $entry->CSYEAR }}</td>
        <td>{{ $entry->REGTYPE }}</td>
        <td>{{ $entry->REGDESCR }}</td>
        <td>{{ $entry->STATUS }}</td>
        <td>{{ $entry->STGROUP }}</td>
        <td>{{ $entry->SEM1 }}</td>
        <td>{{ $entry->SEM2 }}</td>
        <td>{{ $entry->SEM3 }}</td>
        <td>{{ $entry->SEM4 }}</td>
        <td>{{ $entry->SEM5 }}</td>
        <td>{{ $entry->SEM6 }}</td>
        <td>{{ $entry->SEM7 }}</td>
        <td>{{ $entry->SEM8 }}</td>
        <td>{{ $entry->SEM9 }}</td>
        <td>{{ $entry->SEM10 }}</td>
        <td>{{ $entry->SEM11 }}</td>
        <td>{{ $entry->SEM12 }}</td>
        <td>{{ $entry->SEM1B }}</td>
        <td>{{ $entry->SEM2B }}</td>
        <td>{{ $entry->SEM3B }}</td>
        <td>{{ $entry->SEM4B }}</td>
        <td>{{ $entry->SEM5B }}</td>
        <td>{{ $entry->SEM6B }}</td>
        <td>{{ $entry->SEM7B }}</td>
        <td>{{ $entry->SEM8B }}</td>
        <td>{{ $entry->SEM9B }}</td>
        <td>{{ $entry->SEM10B }}</td>
        <td>{{ $entry->SEM11B }}</td>
        <td>{{ $entry->SEM12B }}</td>
        <td>{{ $entry->SEM1P }}</td>
        <td>{{ $entry->SEM2P }}</td>
        <td>{{ $entry->SEM3P }}</td>
        <td>{{ $entry->SEM4P }}</td>
        <td>{{ $entry->SEM5P }}</td>
        <td>{{ $entry->SEM6P }}</td>
        <td>{{ $entry->SEM7P }}</td>
        <td>{{ $entry->SEM8P }}</td>
        <td>{{ $entry->SEM9P }}</td>
        <td>{{ $entry->SEM10P }}</td>
        <td>{{ $entry->SEM11P }}</td>
        <td>{{ $entry->SEM12P }}</td>
        <td>{{ $entry->TOTAL }}</td>
        <td>{{ $entry->TOTALB }}</td>
        <td>{{ $entry->TOTALP }}</td>
    </tr>
@endforeach
  
</table>

</body>
</html>
1 like
konrms's avatar

Laravel version is 5.1.4.

Your code excerpt gave me error: Undefined property: stdClass::$AM (View: C:\xampp\htdocs\laravel\resources\views\pinakas.blade.php)

Vilfago's avatar

Ok... so let's debug

go back in Controller

    public function psaxe(Request $request)
    {

        $myVar = $request->get('AM');
        echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
        echo($myVar);
        $entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
        dd($entries); //add this line
        return view('pinakas', [$entries1 => $entries]);
    }

what do you get ?

konrms's avatar

@VILFAGO - This time I get this:

Αποτελέσματα για τον φοιτητή με ΑΜ: 1030995
array:1 [▼
  0 => {#168 ▼
    +"am": "1030995"
    +"st": "50063549"
    +"cs": "50065646"
    +"regyear": "2004"
    +"regtermin": "1"
    +"csyear": "2004"
    +"regtype": "1"
    +"regdescr": "ΠΑΝΕΛΛΗΝΙΕΣ ΕΞΕΤΑΣΕΙΣ"
    +"status": "Φοίτηση"
    +"stgroup": "79"
    +"sem1": "0"
    +"sem2": "0"
    +"sem3": "0"
    +"sem4": "0"
    +"sem5": "0"
    +"sem6": "0"
    +"sem7": "0"
    +"sem8": "0"
    +"sem9": "0"
    +"sem10": "0"
    +"sem11": "0"
    +"sem12": "0"
    +"sem1b": "0"
    +"sem2b": "0"
    +"sem3b": "0"
    +"sem4b": "0"
    +"sem5b": "0"
    +"sem6b": "0"
    +"sem7b": "0"
    +"sem8b": "0"
    +"sem9b": "0"
    +"sem10b": "0"
    +"sem11b": "0"
    +"sem12b": "0"
    +"sem1p": "0"
    +"sem2p": "0"
    +"sem3p": "0"
    +"sem4p": "0"
    +"sem5p": "0"
    +"sem6p": "0"
    +"sem7p": "0"
    +"sem8p": "0"
    +"sem9p": "0"
    +"sem10p": "0"
    +"sem11p": "0"
    +"sem12p": "0"
    +"total": "0"
    +"totalb": "0"
    +"totalp": "0"
  }
]
Vilfago's avatar
Vilfago
Best Answer
Level 20

Ok, so just delete the dd( line in controller, and in the view, take everything lower case like $entries1->am. It should work.

1 like
konrms's avatar

The problem was the capital letters! Thank you SO MUCH! For your time and everything. The results are as I wanted to show! I paste them below (i don't know if they seem well!)

Αποτελέσματα για τον φοιτητή με ΑΜ: 1030995 AM ST CS REGYEAR REGTERMIN CSYEAR REGTYPE REGDESCR STATUS STGROUP SEM1 SEM2 SEM3 SEM4 SEM5 SEM6 SEM7 SEM8 SEM9 SEM10 SEM11 SEM12 SEM1B SEM2B SEM3B SEM4B SEM5B SEM6B SEM7B SEM8B SEM9B SEM10B SEM11B SEM12B SEM1P SEM2P SEM3P SEM4P SEM5P SEM6P SEM7P SEM8P SEM9P SEM10P SEM11P SEM12P TOTAL TOTALB TOTALP 1030995 50063549 50065646 2004 1 2004 1 ΠΑΝΕΛΛΗΝΙΕΣ ΕΞΕΤΑΣΕΙΣ Φοίτηση 79 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Vilfago's avatar

You're welcome !

For information, best practice you should not have an echo statement in your controller. Anything you want to display should be in the view.

And at the top of your html file (blade view), I changed the language ( <html lang="el"> ), it's better to have the one which is used.

1 like

Please or to participate in this conversation.