John Davidson

php - Show Json data with Laravel

0 comments
Message:


I have a question. Currently, I keep a Json data type in my database, as I show it in the image. The data is named date_table.



where in my controller to extract the data I do it like this:


public function Detalle_serviciosHome(){
$det_serv = Detalle_servicio::all();
$detalles = json_decode($det_serv, true);
dd($detalles);
}

and on my local server it shows it like that



How can I make the data display it and the data from date_table display it in a table? I really appreciate if you could help me.



My Answer:
To display the JSON data from the `date_table` in a table in Laravel, you can follow these steps:

1. Modify your controller method to pass the JSON data to the view:
php
public function Detalle_serviciosHome(){
$det_serv = Detalle_servicio::all();
$detalles = json_decode($det_serv, true);
return view('your_view_name', ['detalles' => $detalles]);
}


2. Create a view file (e.g., `your_view_name.blade.php`) in the `resources/views` directory and add the following code to display the JSON data in a table:
html



@foreach($detalles[0] as $key => $value)

@endforeach



@foreach($detalles as $detalle)

@foreach($detalle as $key => $value)

@endforeach

@endforeach

{{ $key }}
{{ $value }}



3. Access the view in your browser to see the JSON data displayed in a table format.

This code will dynamically create a table with headers based on the keys of the JSON data and populate the table rows with the values. Make sure to adjust the view file and controller method according to your specific requirements and data structure.

Rate this post

5 of 5 based on 1697 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us