Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print result of query in a table in laravel

I have an index method in a controller that returns a collection(https://laravel.com/docs/8.x/collections) to a view

public function index()
{
    $categoriePdf = new CategorieDocumenti;
    $categoriePdf = DB::table('categorie_documenti')
    ->select('id','descrizione','per_veicolo')
    ->get();
    return view('elenca_categoria_pdf', ['campi' => $categoriePdf]);
}

In the view I want to print the result of the query in an adminlte table.

I got the tamplate from here https://github.com/jeroennoten/Laravel-AdminLTE/wiki
and the table i want to setup is this https://github.com/jeroennoten/Laravel-AdminLTE/wiki/Tool-Components#datatables

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the screenshot of how it should be

I want the same thing but not with static data as the example does but with the content of the collection

I tried for hours but I cant understand how to put all the rows of the collection in $config[‘data’] array as the example does with static data:

$config = [
'data' => [
    [22, 'John Bender', '+02 (123) 123456789', '<nobr>'.$btnEdit.$btnDelete.$btnDetails.'</nobr>'],
    [19, 'Sophia Clemens', '+99 (987) 987654321', '<nobr>'.$btnEdit.$btnDelete.$btnDetails.'</nobr>'],
    [3, 'Peter Sousa', '+69 (555) 12367345243', '<nobr>'.$btnEdit.$btnDelete.$btnDetails.'</nobr>'],
],

>Solution :

change your controller to

public function index()
{
    $categoriePdf = new CategorieDocumenti;
    $categoriePdf = DB::table('categorie_documenti')
    ->select('id','descrizione','per_veicolo')
    ->get();
    return view('elenca_categoria_pdf',compact('categoriePdf'));
}

then in your view blade file do something like this

 <thead>
 <th>{{ __('id') }}</th>
<th>{{ __('Name') }}</th>
<th>{{ __('Actions') }}</th>
</thead>
<body>
@foreach($categoriePdf as $categoriePdfs)
<tr>
<td>{{$categoriePdfs->id}}</td>
<td>{{$categoriePdfs->name}}</td>
<td><input type="button" class="btn btn-success" value="add"/></td>
</tr>
@endforeach
 </tbody>
 </table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading