Print Database Data with FOR in Laravel

Asked

Viewed 192 times

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" } }

2 answers

0

With the for you must access the position of the item on array before accessing the property.

@for ($i = 0; $i <= count($result); $i++)
    <tr>
        <td>{{ $result[$i]->nome }}</td>
        <td>{{ $result[$i]->email }}</td>
        <td>{{ $result[$i]->telefone}} </td>
        <td>{{ $result[$i]->morada}} </td>
        <td class="text-center">
            <a href="{{ url("updateUtilizador/{$result[$i]->id}") }}" class="btn btn-primary">Editar</a> 
            <a id="teste{{$result[$i]->id}}" href="{{ url("deleteUtilizador/{$result[$i]->id}") }}" class="btn btn-danger">Apagar</a>
        </td>
    </tr>
@endfor
  • Undefined offset: 2 (View: C: Users Ricardo Desktop Inventory-Management-System-in-Laravel Resources views admin tableUsers users.blade.php)

  • Friend put the var_dump of $resut please.

  • edited the question

  • The problem happened because you have a array of array of objects. You have 3 arrays where each has 2 arrays and in each one you have the data of your object.

  • and how I can solve this?

  • One solution would be for you to make another loop before that.

  • sera can explain in more detail

  • 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" } }

Show 4 more comments

0


To do this you need to change the function of your querry .

 $cases = DB::select('select * from tabela');
 $array = json_decode(json_encode($cases), true);`
 return view('sua view', ['cases' => $array]);

in the above codeline and the querry result is assigned in the form of array.

And to use the array is as Kayo Bruno indicated

   @for ($i = 0; $i < count($cases); $i++)
    <tr>
            <td>{{ $cases[$i]['id'] }}</td>
            <td>{{ $cases[$i]['nome'] }}</td>
            <td>{{ $cases[$i]['email'] }}</td>
            <td>{{ $cases[$i]['telefone'] }} </td>
            <td>{{ $cases[$i]['morada'] }} </td>
            <td class="text-center">
                <a href="{{ url('updateUtilizador/'.$cases[$i]['id']) }}" class="btn btn-primary">Editar</a>
                <a id="teste{{$cases[$i]['id']}}" href="{{ url('deleteUtilizador/'.$cases[$i]['id']) }}" class="btn btn-danger">Apagar</a>
            </td>                                              
    </tr>
@endfor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.