Home > Software design >  Print result of query in a table in laravel
Print result of query in a table in laravel

Time:02-09

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

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>'],
],

CodePudding user response:

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"  value="add"/></td>
</tr>
@endforeach
 </tbody>
 </table>
  •  Tags:  
  • Related