Dan
Dan

Reputation: 17

Send route to view Laravel 8

My Controller returns a JSON from my dbtest function that is a select in my database. I want to display that JSON in a table. I already have a function that does that. My Controller (Controller.php):

public function returnJSON()
{
    $test = new user();
    return response()->json(($teste->dbtest()));
}

My view (welcome). Im using a JSON only for testing, and it works fine. I literally just need to get the JSON from my controller and use it in "$json" variable. I can use a route or anything, I just need to do it. It can't be that hard…;

<body onload ="CreateTableFromJSON()" style=" overflow-x:hidden !important;">
<h1>Olá,<?php echo $name; ?></h1>

@php  
    $json = '[{"COD_RAMAL":"801","COD_USU_RAMAL":"4278","RAMAL":"(31)3379-1050","DESCRICAORAMAL":null,"RAMALCORPORATIVO":"(31)3379-1050","EMAILDOUSUARIO":null},{"COD_RAMAL":"802","COD_USU_RAMAL":"4353","RAMAL":"(33)3333-3333","DESCRICAORAMAL":null,"RAMALCORPORATIVO":"(31)97159-2285","EMAILDOUSUARIO":null},{"COD_RAMAL":"803","COD_USU_RAMAL":"4254","RAMAL":" ","DESCRICAORAMAL":null,"RAMALCORPORATIVO":"","EMAILDOUSUARIO":null},{"COD_RAMAL":"804","COD_USU_RAMAL":"4051","RAMAL":"(31)3369-7032","DESCRICAORAMAL":null,"RAMALCORPORATIVO":"","EMAILDOUSUARIO":null},{"COD_RAMAL":"805","COD_USU_RAMAL":"3923","RAMAL":"(31)3369-7008","DESCRICAORAMAL":null,"RAMALCORPORATIVO":"","EMAILDOUSUARIO":null}]';
    $json_decoded = json_decode($json);
    echo '<table>';
      echo '<tr>';
        echo '<th>cod_ramal</th>';
        echo '<th>codousuramal</th>';
        echo '<th>ramal</th>';
        echo '<th>Descricao</th>';
        echo '<th>ramal corporativo</th>';
        echo '<th>email</th>';
      echo '</tr>';

    foreach($json_decoded as $result){
      echo '<tr>';
        echo '<td>'.$result->COD_RAMAL.'</td>';
        echo '<td>'.$result->COD_USU_RAMAL.'</td>';
        echo '<td>'.$result->RAMAL.'</td>';
        echo '<td>'.$result->DESCRICAORAMAL.'</td>';
        echo '<td>'.$result->RAMALCORPORATIVO.'</td>';
        echo '<td>'.$result->EMAILDOUSUARIO.'</td>';
      echo '</tr>';
    }
    echo '</table>';
    @endphp
</body>       
}

My route:

Route::get('/', function () {
    return view('welcome',['name' =>"test"]);
});

Upvotes: 1

Views: 143

Answers (2)

noah1400
noah1400

Reputation: 1509

Try this Your Route

Route::get('/', [App\Http\Controllers\Controller::class, 'returnJSON']);

Controller.php

public function returnJSON()
{
    #code
    $jsonResponse = $yourResponse;
    $users = json_decode($jsonResponse);
    return view('welcome', compact($users));
}

welcome.blade.php

@foreach ($users as $user)
    <tr>
        <td>{{ $user['detail1'] }}</td>
        <td>{{ $user['detail2'] }}</td>
    </tr>
@endforeach

Upvotes: 1

Vision Coderz
Vision Coderz

Reputation: 8078

As per my understanding you can return view from controller.So you can keep your view file clean using blade template

public function returnJSON()
{
   $test = new user();
   return view('welcome',['userDetail'=>$teste->dbtest()]);
}

welcome.blade.php

<table>
    <thead>
    <tr>
        <th>cod_ramal</th>
        <th>codousuramal</th>
        <th>ramal</th>
        <th>Descricao</th>
        <th>ramal corporativo</th>
        <th>email</th>
    </tr>
    </thead>
<tbody>
@foreach($userDetail as $result)
    <tr>
        <td>{{$result->COD_RAMAL}}</td>
        <td>{{$result->COD_USU_RAMAL}}</td>
        <td>{{$result->RAMAL}}</td>
        <td>{{$result->DESCRICAORAMAL}}</td>
        <td>{{$result->RAMALCORPORATIVO}}</td>
        <td>{{$result->EMAILDOUSUARIO}}</td>
    </tr>

@endforeach
</tbody>

</table>

Upvotes: 1

Related Questions