Looping through json laravel - php

I have a table in which a column stores json data,but I am unable to loop through it in the view.
Sample Data :
[
{
"id":28,
"item_id":4,
"item_name":"Texco Dx",
"rate":"15.00",
"unit":"box",
"qty":"20",
"price":300,
"tax":12,
"taxType":"GST",
"tax_paid":36,
"disc":0,
"value":336,
"barcode":"1234ergeg",
"hsn":"q23423",
"lot_number":"1235r23r",
"checked":true,
"error":false,
"returnedQty":12,
"returnedValue":33.6,
"returnedTax":3.6,
"allowedQty":10
},
{
"id":27,
"item_id":3,
"item_name":"Norflox Tz",
"rate":"124.00",
"unit":"qty",
"qty":"10",
"price":1240,
"tax":10,
"taxType":"GST",
"tax_paid":124,
"disc":0,
"value":1364,
"barcode":"11121231",
"hsn":"123213",
"lot_number":"123",
"checked":true,
"error":false,
"returnedQty":3,
"returnedValue":272.8,
"returnedTax":24.8,
"allowedQty":9
}
]
This is how I am trying to loop through it :
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill[0]->ITEM_DETAILS as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item0->item_name}}</td>
</tr>
#endforeach
</table>
When I stored the same data using ng-init and tried iterating through it using ng-repeat it worked.
Can anyone explain this behaviour.
Thanks in advance

If you are getting a json in the view and assuming that $bill contains the json:
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach (json_decode($bill, true) as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>
If your json comes from the controller you could pass an array and do this.
In controller
$bills = ....
return view('your.template', ['bill' => json_decode($bills, true)]);
In view
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>

Related

laravel json data listing

I'm getting json data but I can't list it in datatable
Controller File
public function index()
{
$data = Http::get('https://jsonplaceholder.typicode.com/posts');
return view('frontend.default.index', ['data'=> $data->json()]);
}
View File
<table id="table_id" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $value)
<tr>
<td>
{{$value->title}} // not working
</td>
<td>Row 1 Data 2</td>
</tr>
#endforeach
</tbody>
</table>
The data you pull from json comes as an array. You are trying to print as an object while you print.
Solution
#foreach ($data as $key => $value)
<tr>
<td>
{{$value['title']}} // not working
</td>
<td>Row 1 Data 2</td>
</tr>
#endforeach
basically you use JSON data types for ajax since they cannot pass array or object data without JSON but when you are directly printing the value in the view then you do not need to convert the data to JSON. here is how you can use it.
public function index()
{
$data = Http::get('https://jsonplaceholder.typicode.com/posts');
//check if data is being received
if(!$data)
return back()->with('error', 'no data received');
return view('frontend.default.index', compact('data'));
}
and in the front end......
<table id="table_id" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $value)
<tr>
<td>
{{$value->title}}
</td>
<td>Row 1 Data 2</td>
</tr>
#endforeach
</tbody>
</table>
or
<table id="table_id" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $value)
<tr>
<td>
{{$value['title']}}
</td>
<td>Row 1 Data 2</td>
</tr>
#endforeach
</tbody>
</table>

How to automatically number each row in a table in laravel?

Please I want to automatically number each row when data is displayed from the database. Something like this
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
so each row is numbered in ascending order from 1 to n depending on the data available in the database. How do I properly do that. Any Help please am new in laravel
If you are using blade, the laravel's automatically generated $loop variable might help. Like this:
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
#foreach($items as $item)
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $item->first_name }}</td>
<td>{{ $item->last_name }}</td>
<td>#{{ $item->username }}</td>
</tr>
#endforeach
</tbody>
</table>
You can find documentation here: https://laravel.com/docs/8.x/blade#the-loop-variable

laravel 5.4 table guidance needed

Hello i am a laravel beginner , when i do foreach its shows double of all single items what should i do with it code and ss are below
Brower image
code
<div >
<h1 style="text-align: center">Lists of Outlets</h1>
#foreach($outlets as $outlet)
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
</tbody>
</table>
#endforeach
</div>
The issue is, you have put the entire table inside the loop, which is wrong. You have to only put the tr inside the loop.
Try this:
#foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
#endforeach
and one more thing, you are also using the static content inside the loop instead of using the loop variable value. So instead of:
<td>john#example.com</td>
it is something like:
<td>{{ $outlet['name'] }}</td> <!-- where name is the index in outlet array -->
#foreach should be inside <tbody> and instead of hard-coded content, you should be using $outlet variable to get firstname, lastname and Email:
<div>
<h1 style="text-align: center">Lists of Outlets</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
#endforeach
</tbody>
</table>
</div>

DataTables form with checkboxes

I'm trying to create DataTable in CodeIgniter with data from MySql. I'm not sure how to create form on DataTable that will handle checkboxes on each row.
HTML
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Discount</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Discount</th>
</tr>
</tfoot>
<tbody>
<?php foreach($data as $d): ?>
<tr>
<td></td>
<td><?=$d->name?></td>
<td><?=$d->price?></td>
<td><?=$d->discount?>%</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

How can I add a link to a table in Smarty?

I have a requirement to output a table using Smarty and then have the user select a row to take them to another page.
I can display the table fine:
{html_table cols="Job Title,Salary,Sector,Location" loop=$results}
which gives:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
</tr>
</tbody>
</table>
but I am not sure if it is possible to add a hyperlink as an additional column to the table that links to a page using a hidden id.
So I would want something link this:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
<th>Apply</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
<td>Apply</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
<td>Apply</td>
</tr>
</tbody>
</table>
Is that possible?
Yes, but you need to modify the $results array before you pass it to Smarty so that the 4th element of each row contains the link as a string. There's no way to have {html_table} generate the link for you.

Categories