0
I am a beginner in the framework Laravel, in which I have a doubt, when printing database records for an html table, in all the examples I saw was used the foreach
, I have also used.
@foreach($result as $data)
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->nome }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->telefone}} </td>
<td>{{ $data->morada}} </td>
<td class="text-center"><a href="{{ url('updateUtilizador/'.$data->id) }}" class="btn btn-primary">Editar</a> <a id="teste{{$data->id}}" href="{{ url('deleteUtilizador/'.$data->id) }}" class="btn btn-danger">Apagar</a> </td>
</tr>
@endforeach
But I at this time of "training the framework" I wanted to use the structure for
instead of the foreach
, currently this and the code I have with the for, is going to fetch all the data well, I just don’t know how to print.
@for ($i = 0; $i <= count($result); $i++)
<?php var_dump($resut); ?>
<tr>
<td>{{ $result[$i][nome] }}</td>
<td>{{ $data->nome }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->telefone}} </td>
<td>{{ $data->morada}} </td>
<td class="text-center"><a href="{{ url('updateUtilizador/'.$data->id) }}" class="btn btn-primary">Editar</a> <a id="teste{{$data->id}}" href="{{
url('deleteUtilizador/'.$data->id) }}" class="btn btn-danger">Apagar</a> </td>
</tr>
@endfor
VAR_DUMP of the variable
array(2) { [0]=> object(stdClass)#287 (5) { ["id"]=> int(16) ["nome"]=> string(1) "e" ["email"]=> string(1) "e" ["telefone"]=> string(1) "e" ["morada"]=> string(1) "e" } [1]=> object(stdClass)#289 (5) { ["id"]=> int(19) ["nome"]=> string(2) "cc" ["email"]=> string(1) "c" ["telefone"]=> string(1) "c" ["morada"]=> string(1) "c" } } array(2) { [0]=> object(stdClass)#287 (5) { ["id"]=> int(16) ["nome"]=> string(1) "e" ["email"]=> string(1) "e" ["telefone"]=> string(1) "e" ["morada"]=> string(1) "e" } [1]=> object(stdClass)#289 (5) { ["id"]=> int(19) ["nome"]=> string(2) "cc" ["email"]=> string(1) "c" ["telefone"]=> string(1) "c" ["morada"]=> string(1) "c" } } array(2) { [0]=> object(stdClass)#287 (5) { ["id"]=> int(16) ["nome"]=> string(1) "e" ["email"]=> string(1) "e" ["telefone"]=> string(1) "e" ["morada"]=> string(1) "e" } [1]=> object(stdClass)#289 (5) { ["id"]=> int(19) ["nome"]=> string(2) "cc" ["email"]=> string(1) "c" ["telefone"]=> string(1) "c" ["morada"]=> string(1) "c" } }
Undefined offset: 2 (View: C: Users Ricardo Desktop Inventory-Management-System-in-Laravel Resources views admin tableUsers users.blade.php)
– user174268
Friend put the
var_dump
of$resut
please.– Kayo Bruno
edited the question
– user174268
The problem happened because you have a
array
ofarray
of objects. You have 3arrays
where each has 2arrays
and in each one you have the data of your object.– Kayo Bruno
and how I can solve this?
– user174268
One solution would be for you to make another loop before that.
– Kayo Bruno
Let’s go continue this discussion in chat.
– user174268
sera can explain in more detail
– user174268
array(2) { [0]=> array(5) { ["id"]=> int(16) ["name"]=> string(1) "e" ["email"]=> string(1) "e" ["telephone"]=> string(1) "e" ["address"]=> string(1) "e" } [1]=> array(5) { ["id"]=> int(19) ["name"]=> string(2) "cc" ["email"]=> string(1) "c" ["telephone"]=> string(1) "c" ["address"]=> string(1) "c" } } array(2) { [0]=> array(5) { ["id"]=> int(16) ["name"]=> string(1) "e" ["email"]=> string(1) "e" ["phone"]=> string(1) "e" ["address"]=> string(1) "e" } [1]=> array(5) { ["id"]=> int(19) ["name"]=> string(2) "cc" ["email"]=> string(1) "c" ["telephone"]=> string(1) "c" ["address"]=> string(1) "c" } }
– user174268