I have an array that i want to return from the controller to the view.But unable to retrieve that.
If i dump my array it looks like this:
array:121 [▼
0 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "Bangalore"
"BookingId" => "EZBK000126"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "1110002223"
]
1 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "mysore"
"BookingId" => "EZBK000125"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "9146178526"
]
So i want to get it to show inside my view .Currently my code looks like this :
#foreach($new_records as $new_recordss)
<tr>
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
<td scope="col">{{ $new_recordss->Status }}</td>
<td scope="col">{{ $new_recordss->Age }}</td>
<td scope="col"><div class="btn-group">
</div>
</td>
</tr>
#endforeach
But this gives the following error:
Trying to get property of non-object
That's because an array is not an object.
// -> is for object
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
// [] is for array
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
Thus this should work:
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
<td scope="col">{{ $new_recordss['Status'] }}</td>
<td scope="col">{{ $new_recordss['Age'] }}</td>
The reason why
{{ $new_recordss->ProcedureName }}
would not work is because -> operator is used to access an Object. Notice that you get the following error:
Trying to get property of non-object
Because you have an array with 121 arrays in it. Therefore in order to access this associative array, you would use
{{ $new_recordss['ProcedureName'] }}
Here, the 'ProcedureName' is simply the key name and we are telling the array to fetch it's value. I hope this answers your question.
Related
This question already has answers here:
How to loop multidimensional array in laravel blade
(3 answers)
Closed 4 months ago.
return view($this->folder_path . 'index', compact('data' ));
I want to use foreach to display data.
array:5 [▼
0 => array:4 [▶]
1 => array:4 [▼
"id" => 2
"name" => "test"
"desc" => "my name"
"age" => 12
]
This is view where i want to display data
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item->name) }}</td>
<td>{{ $item->desc }}</td>
<td>{{ $item->age }}</td>
<tr>
#endforeach
Your $data and each $item appear to be arrays and not objects, therefore using the object accessor (->) on $item will not work. Instead you should be accessing the elements of your $item array using their key names:
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item['name']) }}</td>
<td>{{ $item['desc'] }}</td>
<td>{{ $item['age'] }}</td>
<tr>
#endforeach
Probably worthwhile doing some reading on associative arrays.
I have some data that returns to me as a laravel array and I want to print it out in a table in a view, but I can't access that data
array:1 [▼
0 => array:2 [▼
0 => array:3 [▼
0 => "MS3939"
1 => "20G1"
2 => "20DC"
]
1 => array:3 [▼
0 => "MRK940"
1 => "20R1"
2 => "20RF"
]
]
]
this is my view code with the table
<table class="table table-bordered">
<thead>
<tr class="text-center">
<th>ID</th>
<th>ISO</th>
<th>Type</th>
</tr>
</thead>
<tbody id="filter-table">
#foreach($datos as $dato)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>{{$dato['0']}}</td>
<td>{{$dato['1']}}</td>
<td>{{$dato['2']}}</td>
#endforeach
</tr>
</tbody>
</table>
If your $datos is a collection then do one thing.
$datos = $datos->flatten(1); //it will reduce one level of your array
If your $datos is not a collection and it just an array then convert it to collection
$datos = collect($datos); //then
$datos = $datos->flatten(1);
Then in view you are already running a loop, so you will be able to access data like...
<td>{{ $dato[0] }}</td>
<td>{{ $dato[1] }}</td>
<td>{{ $dato[2] }}</td>
I try to display my pagination data to database ...My pagination data was from json that i already decode .....i was able to pass to my pagination data to view but not able to display in table
....
This is my error from blade view
Trying to get property of non-object
my result pagination
LengthAwarePaginator {#4177 ▼
#total: 1
#lastPage: 1
#items: Collection {#4169 ▼
#items: array:1 [▼
"data" => array:852 [▼
0 => {#624 ▶}
1 => {#623 ▶}
2 => {#628 ▼
+"ID": "cust1"
+"PWD": "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
+"NICK_NAME": "CUST1"
+"PWD_INVALID_HIT": "0"
+"PWD_CREATE_DT": "2019/07/22 15:57:15"
+"INSTITUTION_ID": "C01"
+"ACL_ID": "L04"
+"LOGIN_DT": "2019/07/22 15:57:15"
+"LOGIN_STS_ID": "N"
+"STS_ID": "C08"
+"TYPE_ID": "U00"
+"UPD_ID": "sufyzasukor"
+"UPD_DT": "2019/07/22 15:57:15"
+"EMAIL_ID": "cust1#gmail.com"
+"PHONE_NO_ID": "0"
+"HP_ID": "0 "
+"CRT_DATE_DELETED": null
+"irel__com_access_level": {#621 …9}
+"irel__com_user_types": {#630 …5}
+"irel__com_status": {#631 …5}
}
}
#perPage: "15"
#currentPage: 1
#path: "http://localhost/IFICSV3/public/configuration/comuserprofiles"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
My controller
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$total = count($content) ;
$collection = new \Illuminate\Support\Collection($content);
$paginationRecord = CollectionPaginate::paginate($collection, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
my blade table that i want to display
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user->ID }}</td>
<td>{{ $user->NICK_NAME }}</td>
<td>{{ $user->PWD_INVALID_HIT }}</td>
</tr>
#endforeach
dd($user)
array:852 [▼
0 => array:20 [▶]
1 => array:20 [▶]
2 => array:20 [▼
"ID" => "cust1"
"PWD" => "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
"NICK_NAME" => "CUST1"
"PWD_INVALID_HIT" => "0"
"PWD_CREATE_DT" => "2019/07/22 15:57:15"
"INSTITUTION_ID" => "C01"
"ACL_ID" => "L04"
"LOGIN_DT" => "2019/07/22 15:57:15"
"LOGIN_STS_ID" => "N"
"STS_ID" => "C08"
"TYPE_ID" => "U00"
"UPD_ID" => "sufyzasukor"
"UPD_DT" => "2019/07/22 15:57:15"
"EMAIL_ID" => "cust1#gmail.com"
"PHONE_NO_ID" => "0"
"HP_ID" => "0 "
"CRT_DATE_DELETED" => null
"irel__com_access_level" => array:9 [ …9]
"irel__com_user_types" => array:5 [ …5]
"irel__com_status" => array:5 [ …5]
How do i display data in table
You are trying to access a array as a object . That's why the error . Try this .
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
I think it's cause you have a collection in your $user variable. You need to loop over that $user var like this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
#foreach($user as $i => $u)
<tr>
<td>{{ $u['ID'] }}</td>
<td>{{ $u['NICK_NAME'] }}</td>
<td>{{ $u['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
#endforeach
Also, do you need this:
#php
$currentRecordno = 1;
#endphp
The $i in your foreach loop should represent the index or $currentRecordno. But if you have good reason, then stick with it.
For readability:
But really, I probably wouldn't do that extra foreach loop.
I think your controller method should be more like:
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$paginationRecord = CollectionPaginate::paginate($content['data'], count($content), '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
Actually your blade will need to change to this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
I have the below arrays and I want to get it in laravel blade as HTML table
array:2 [▼
"chest" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "1"
]
"Flys" => array:2 [▼
0 => "3"
1 => "4"
]
]
"hints" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "test1"
]
"Flys" => array:1 [▼
0 => "test2"
]
]
]
I try the below but I dont get right HTML table, its correct the first two columns but the third columnt is not, any ideas how to print it on HTML table
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th>Exercises</th>
<th>Days</th>
<th>Hints</th>
</tr>
</thead>
<tbody>
#foreach($chests['chest'] as $chest => $exc)
<tr>
<td>{{$chest}}</td>
#foreach($exc as $key => $value)
<td>
<strong>{{$value}},</strong>
</td>
#endforeach
#endforeach
#foreach($chests['hints'] as $hint => $hin)
#foreach($hin as $key => $value)
<td>
<strong>{{$value}}</strong>
</td>
#endforeach
</tr>
#endforeach
</tbody>
Answered
<tbody>
#foreach ($chests['chest'] as $exerciseName => $daysArray)
<tr>
<td>{{ $exerciseName }}</td>
#foreach ($daysArray as $day)
<td >{{ $day }}</td>
#endforeach
<td>{{ $chests['hints'][$exerciseName][0] }}</td>
</tr>
#endforeach
</tbody>
https://laracasts.com/discuss/channels/laravel/laravel-blade-multidimensional-array
I have an array like so:
array:55 [▼
0 => array:13 [▼
"product_id" => "1"
"name" => "Glowing Antique Multi-Color Necklace And Hangings"
"product_code" => "DIV-COL-0001-0001"
"option_code" => ""
"net_price" => "693.00"
"sellerCommission" => "450.00"
"moderatorCommission" => "14.00"
"principalModeratorCommission" => "17.00"
"affiliateCommission" => "28.00"
"accountType" => "affiliate"
]
25 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
26 => array:13 [▼
"product_id" => "25"
"name" => "Side Hanging Purse (2 Nos.)"
"product_code" => "PRA-KEN-0002-0007"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
44 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "principal_moderator"
]
// And the list continues ...
]
The code that I have tried so far, works correctly to a certain extent.
<tr>
<th style="vertical-align: top;">Id</th>
<th style="vertical-align: top;">Details</th>
<th style="vertical-align: top;">Net Price</th>
<th style="vertical-align: top;">Invoicer<br /> Commission</th>
<th style="vertical-align: top;">Moderator Commission</th>
<th style="vertical-align: top;">Principal Moderator Commission</th>
<th style="vertical-align: top;">Affiliate Commission</th>
</tr>
#foreach($products as $product)
<tr>
<td>{{ $product['product_id'] }}</td>
<td>
<img src="{{ $product['image'] }}" alt="{{ $product['name'] }}" class="pull-left" style="margin-right: 15px">
{{ $product['name'] }}<br />
Product Code: {{ $product['product_code'] }}<br />
#if($product['option_code'] !== '')
Option Code: {{ $product['option_code'] }}
#endif
</td>
<td class="text-right">{{ $product['net_price'] }}</td>
#if($product['accountType'] === 'seller')
<td class="text-right">{{ number_format($product['sellerCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'moderator')
<td class="text-right">{{ number_format($product['moderatorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'principal_moderator')
<td class="text-right">{{ number_format($product['principalModeratorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'affiliate')
<td class="text-right">{{ number_format($product['affiliateCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
</tr>
#endforeach
The above code works, but it adds a new row to the table which is what I am not looking for. I do not want to add a new row, instead I would like to display the required data in that row only.
Meaning, for the product_id = 24, (except for the option code), there are 2 accountType attached viz., affiliate and principal_moderator. I would like to populate a single row with that data instead of 2 different rows.
How do I achieve it ??
I know this must be far easy, but I have failed to solve it, because I am still at the learning stage.
Any help is highly appreciated. Thanks.
P.S.: All the values are coming from the database, and the number of child arrays can be infinite.
You should group rows and make accountType property an array. Basically create new array, loop through products and add them into new array if they are not already added and if they are then add accountType to it. Something like this:
$grouped = array();
foreach($products as $product) {
if(!array_key_exists($product['product_id'], $grouped)) {
$grouped[$product['product_id']] = $product;
$grouped[$product['product_id']]['accountTypes'] = array($product['accountType']);
}else {
$grouped[$product['product_id']]['accountTypes'][] = $product['accountType'];
}
}
Then you use grouped data to display data and on accountType check you check if that type exists in accountTypes property.