Error when show data from mySQL to View in Laravel - php

I use Laravel to show data from Mysql to View by multi-queries.
Here is my Route:
Route::get('/procinstdetail','ProcDetailController#index');
And here is my controller:
use App\act_hi_taskinst;
use View;
class ProcDetailController extends Controller
{
private $act_hi_taskinst;
public function __construct(act_hi_taskinst $act_hi_taskinst) {
$this -> act_hi_taskinst = $act_hi_taskinst;
}
public function index(act_hi_taskinst $act_hi_taskinst, Request $request)
{
$procinstid = $request->get('id');
return View::make('datatrackingdetail')->with('data', $this->act_hi_taskinst->getData($procinstid));
}
}
And my model:
class act_hi_taskinst extends Model
{
public function getData($procinstid) {
$data = array();
$data['taskData'] = DB::select("SELECT NAME_, PRIORITY_, ASSIGNEE_, DUE_DATE_, START_TIME_, END_TIME_, PROC_DEF_ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ = $procinstid");
$data['varData'] = DB::select("SELECT NAME_, TEXT_ FROM act_hi_varinst WHERE PROC_INST_ID_ = $procinstid");
$data['subprocessData'] = DB::select("SELECT * FROM act_hi_procinst WHERE SUPER_PROCESS_INSTANCE_ID_ = $procinstid");
return $data;
}
}
Final is fetch data to view:
<body>
<h2>Task</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>Name</th>
<th>Priority</th>
<th>Assignee</th>
<th>Due Date</th>
<th>Created</th>
<th>Completed</th>
</tr>
#if(isset($data))
#foreach($data as $processTaskDataValue)
<tr>
<td>{{ $processTaskDataValue -> NAME_ }}</td>
<td>{{ $processTaskDataValue -> PRIORITY_ }}</td>
<td>{{ $processTaskDataValue -> ASSIGNEE_ }}</td>
<td>{{ $processTaskDataValue -> DUE_DATE_ }}</td>
<td>{{ $processTaskDataValue -> START_TIME_ }}</td>
<td>{{ $processTaskDataValue -> END_TIME_ }}</td>
</tr>
#endforeach
#endif
</table>
<h2>Variable</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
#if(isset($data))
#foreach($data as $processVarDataValue)
<tr>
<td>{{ $processVarDataValue -> NAME_ }}</td>
<td>{{ $processVarDataValue -> TEXT_ }}</td>
</tr>
#endforeach
#endif
</table>
<h2>Sub Process</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>ID_</th>
<th>PROC_INST_ID_</th>
<th>BUSINESS_KEY_</th>
<th>PROC_DEF_ID_</th>
<th>START_TIME_</th>
<th>END_TIME_</th>
<th>DURATION_</th>
<th>START_USER_ID_</th>
<th>START_ACT_ID_</th>
<th>END_ACT_ID_</th>
<th>SUPER_PROCESS_INSTANCE_ID_</th>
<th>DELETE_REASON_</th>
<th>TENANT_ID_</th>
<th>NAME_</th>
</tr>
#if(isset($data))
#foreach($data as $subprocessDataValue)
<tr>
<td> {{$subprocessDataValue -> ID_ }}</td>
<td>{{ $subprocessDataValue -> PROC_INST_ID_ }}</td>
<td>{{ $subprocessDataValue -> BUSINESS_KEY_ }}</td>
<td>{{ $subprocessDataValue -> PROC_DEF_ID_ }}</td>
<td>{{ $subprocessDataValue -> START_TIME_ }}</td>
<td>{{ $subprocessDataValue -> END_TIME_ }}</td>
<td>{{ $subprocessDataValue -> DURATION_ }}</td>
<td>{{ $subprocessDataValue -> START_USER_ID_ }}</td>
<td>{{ $subprocessDataValue -> START_ACT_ID_ }}</td>
<td>{{ $subprocessDataValue -> END_ACT_ID_ }}</td>
<td>{{ $subprocessDataValue -> SUPER_PROCESS_INSTANCE_ID_ }}</td>
<td>{{ $subprocessDataValue -> DELETE_REASON_ }}</td>
<td>{{ $subprocessDataValue -> TENANT_ID_ }}</td>
<td>{{ $subprocessDataValue -> NAME_ }}</td>
</tr>
#endforeach
#endif
</table>
</body>
But I receive error:
Trying to get property 'NAME_' of non-object.
What's wrong with my code?
I'm new with Laravel so I really don't know how to fix it.

In your Model you are creating array with key taskData, varData and subprocessData and you forgot to use this key to your $data array while getting data
<body>
<h2>Task</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>Name</th>
<th>Priority</th>
<th>Assignee</th>
<th>Due Date</th>
<th>Created</th>
<th>Completed</th>
</tr>
#if(isset($data))
#foreach($data['taskData'] as $processTaskDataValue)
<tr>
<td>{{ $processTaskDataValue->NAME_ }}</td>
<td>{{ $processTaskDataValue->PRIORITY_ }}</td>
<td>{{ $processTaskDataValue->ASSIGNEE_ }}</td>
<td>{{ $processTaskDataValue->DUE_DATE_ }}</td>
<td>{{ $processTaskDataValue->START_TIME_ }}</td>
<td>{{ $processTaskDataValue->END_TIME_ }}</td>
</tr>
#endforeach
#endif
</table>
<h2>Variable</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
#if(isset($data))
#foreach($data['varData'] as $processVarDataValue)
<tr>
<td>{{ $processVarDataValue->NAME_ }}</td>
<td>{{ $processVarDataValue->TEXT_ }}</td>
</tr>
#endforeach
#endif
</table>
<h2>Sub Process</h2>
<table border="1" cellspacing = "0" cellpadding = "3">
<tr>
<th>ID_</th>
<th>PROC_INST_ID_</th>
<th>BUSINESS_KEY_</th>
<th>PROC_DEF_ID_</th>
<th>START_TIME_</th>
<th>END_TIME_</th>
<th>DURATION_</th>
<th>START_USER_ID_</th>
<th>START_ACT_ID_</th>
<th>END_ACT_ID_</th>
<th>SUPER_PROCESS_INSTANCE_ID_</th>
<th>DELETE_REASON_</th>
<th>TENANT_ID_</th>
<th>NAME_</th>
</tr>
#if(isset($data))
#foreach($data['subprocessData'] as $subprocessDataValue)
<tr>
<td><a href= "{{ url('/procinstdetail') }}?id={{
$subprocessDataValue -> ID_ }}&endtime={{ $subprocessDataValue ->
END_TIME_ }}"> {{$subprocessDataValue -> ID_ }}</a></td>
<td>{{ $subprocessDataValue->PROC_INST_ID_ }}</td>
<td>{{ $subprocessDataValue->BUSINESS_KEY_ }}</td>
<td>{{ $subprocessDataValue->PROC_DEF_ID_ }}</td>
<td>{{ $subprocessDataValue->START_TIME_ }}</td>
<td>{{ $subprocessDataValue->END_TIME_ }}</td>
<td>{{ $subprocessDataValue->DURATION_ }}</td>
<td>{{ $subprocessDataValue->START_USER_ID_ }}</td>
<td>{{ $subprocessDataValue->START_ACT_ID_ }}</td>
<td>{{ $subprocessDataValue->END_ACT_ID_ }}</td>
<td>{{ $subprocessDataValue->SUPER_PROCESS_INSTANCE_ID_ }}</td>
<td>{{ $subprocessDataValue->DELETE_REASON_ }}</td>
<td>{{ $subprocessDataValue->TENANT_ID_ }}</td>
<td>{{ $subprocessDataValue->NAME_ }}</td>
</tr>
#endforeach
#endif
</table>
</body>

DB::select() will return an Array. So call it's property accordingly.
small POC
Update: Also as per other answer, you are not iterating over elements of $data, not actual data that you get from DB::select.

Related

Using DataTables in Laravel without aJax - No data available in table

I have a regular Laravel view that renders a table. When I try to use Datatables on it, it does render the table but it seems to want to format it before the table is actually rendered. I'm confused because, by the time the view is rendered, the server-side work is already done. The only thing it's doing is to render the table with the object passed from the controller.
<table id="stockmovement" class="table table-hover" style="width:100%">
<tbody>
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
<th colspan="3"></th>
</thead>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
$(document).ready(function() {
$('#stockmovement').DataTable();
})
This is what I end up with:
I don't want to use AJAX to form my table. I've used plenty of vanilla PHP and DataTables instances before where it works just fine. I would appreciate any pointers on what I may be missing.
You have placed <tbody> in the wrong place. <tbody> should use under the </thead>.
Here is the code example:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
Add tr tag inside the thead tag.
The foreach loop should be in the opening and closing tbody tag.
It should look like this:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</tr>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>

Trying to access to array in view return Trying to get property of non-object

I'm using Laravel 5.2, I'm trying to execute a stored procedure and display the result in a view using a foreach loop. When I run my app I get the following error message:
Trying to get property of non-object
If I use return or dump I get the following result:
Here is my controller code:
public function VentasPorSucursal() {
$values = [1, 25, 2, "NULL", "2018-08-10", "2018-08-10", "NULL", "NULL"];
$data['data'] = DB::select('SP_RPT_VENTAS2 ?, ?, ?, ?, ?, ?, ?, ?', $values);
//dump($data);
return view('reportes.index')->with('data', $data);
}
And this is my index.blade.php
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed table-hover">
<thead>
<th>Folio</th>
<th>Fecha</th>
<th>Sucursal</th>
<th>Vendedor</th>
<th>Cantidad</th>
<th>Unidad</th>
<th>DescripciĆ³n</th>
<th>Pzaz x Pq</th>
<th>Precio</th>
<th>Total</th>
<th>Total Pzaz. Vendidas</th>
</thead>
#foreach ($data as $result)
<tr>
<td>{{ $data->Folio }}</td>
<td>{{ $data->Fecha }}</td>
<td>{{ $data->Sucursal }}</td>
<td>{{ $data->Vendedor }}</td>
<td>{{ $data->Cantidad }}</td>
<td>{{ $data->Unidad }}</td>
<td>{{ $data->Descripcion }}</td>
<td>{{ $data->PzasxPq }}</td>
<td>{{ $data->Precio }}</td>
<td>{{ $data->Total }}</td>
<td>{{ $data->TotalPzasVendidas }}</td>
</tr>
#endforeach
</table>
</div>
{{$data->render()}}
Looking for an answer I found that using '->' it's for an array and maybe my code is returning the result as an object and I need to use '[]' but I don't know exactly how can I do it.
What could be the problem?
Looking at your dump result. The data array you are passing to view contains a data key which contains all of your objects returned from query. So you can loop through data variable data objects like this:
#foreach ($data['data'] as $result)
<tr>
<td>{{ $result->Folio }}</td>
<td>{{ $result->Fecha }}</td>
<td>{{ $result->Sucursal }}</td>
<td>{{ $result->Vendedor }}</td>
<td>{{ $result->Cantidad }}</td>
<td>{{ $result->Unidad }}</td>
<td>{{ $result->Descripcion }}</td>
<td>{{ $result->PzasxPq }}</td>
<td>{{ $result->Precio }}</td>
<td>{{ $result->Total }}</td>
<td>{{ $result->TotalPzasVendidas }}</td>
</tr>
#endforeach
Your first problem is in foreach loop which i reset is below and secondly
If the return result is an array it can be access like below.
#foreach ($data as $result)
<tr>
<td>{{ $result["Folio"]}}</td>
<td>{{ $result["Fecha"] }}</td>
<td>{{ $result["Sucursal"] }}</td>
<td>{{ $result["Vendedor"] }}</td>
<td>{{ $result["Cantidad"] }}</td>
<td>{{ $result["Unidad"] }}</td>
<td>{{ $result["Descripcion"] }}</td>
<td>{{ $result["PzasxPq"] }}</td>
<td>{{ $result["Precio"] }}</td>
<td>{{ $result["Total"] }}</td>
<td>{{ $result["TotalPzasVendidas"] }}</td>
</tr>
#endforeach

Foreach loop (laravel blade)

I need help with a foreach loop in laravel. For some reason I need to have two rows instead of two tables (would have solved my problem) However, I have problems with it getting to work due to not knowing where to place my tags to get the right table structure.
Code:
<table class="minimalistBlack">
<tbody>
<tr>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
<th>X</th>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
</tr>
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
#foreach ($data2 as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
#endforeach
</tbody>
</table>
How can I combine the two foreach loops and and get the right table structure?
to achieve just that result you could do
foreach (array_combine($courses, $sections) as $course => $section)
but that only works for two arrays
or u can use two table and make feel as single table
<div class="row">
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
Assuming the $data and $data2 variables are Laravel collection instances, you can simply use $data = $data->concat($data2). Now $data will contain data of both variables. For a simple array, you can use $data += $data2;. This will merge $data & $data2 to $data. Then you can simply use:
...
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach
</tbody>
</table>
User array_merge function in controller like this
$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);
if $data and $data2 have one, one row you can try like this.
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
</tr>
#endforeach
#foreach ($data2 as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach

JSON output to AngularJS parsejson

I cannot seem to put these into a scope.. And not sure if im doing something wrong...
HTML:
<div class="col-md-10 pull-md-left" style=" width:102%;">
<div class="row">
<span style="text-decoration:underline;">EC2 Instances</span> <span style="clear:both;">refresh</span>
<table class="table-striped" style="width:100%;border:1px solid gray;">
<thead>
<tr style="border-bottom:1px solid black; background-color:black; color:white;">
<th title="ID" style="width:20px;">#</th>
<th title="Customer Attached">Customer</th>
<th title="Instance Name">Name</th>
<th title="Instance ID">ID</th>
<th title="Instance Type">Type</th>
<th title="Instance Availability Zone">Zone</th>
<th title="This is Title">State</th>
<th title="Public DNS">WAN_DNS</th>
<th title="Public IP">WAN_IP</th>
<th title="What Instasnce Was Booted Up">Started</th>
<th title="Security Groups">Security</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="ec2 in servers">
<td>{{ $index + 1 }}</td>
<td>{{ customerID }}</td>
<td>{{ instancename }}</td>
<td>{{ ec2.instanceID }}</td>
<td>{{ ec2.instanceType }}</td>
<td>{{ ec2.instanceAvailabilityZone }}</td>
<td>{{ ec2.instanceState }}</td>
<td>{{ ec2.publicdns }}</td>
<td>{{ ec2.publicip }}</td>
<td>{{ ec2.startdate }}</td>
<td>{{ ec2.secgroup }}</td>
</tr>
<tr ng-show="!ec2.length">
<td colspan="11" style="text-align:center;">
<BR>
No Instances have been syncronized.
<BR>
<BR>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
CONTROLLER:
<script>
var app = angular.module('myApp', []);
app.controller('ec2instanceCtrl', function($scope, $http) {
console.log('a test');
$http.get("ec2instances.php")
.then(function (response) {
$scope.servers = JSON.parse(response.data);
//$scope.servers = response.data.records;
$scope.servers = results[0].ec2instance.map;
}
);
});
JSON:
{
"ec2instance": [{
"ID": 0,
"customerID": null,
"ec2.instancename": "domain1.host",
"ec2.instanceID": "i...",
"ec2.instanceType": "x3large",
"ec2.instanceAvailabilityZone": "us-east-1a",
"ec2.instanceState": "running",
"ec2.publicdns": "..com",
"ec2.publicip": "127.0.0.1",
"ec2.startdate": "11\/25\/2016 1:30AM",
"ec2.secgroup": "open.ports"
}, {
"ID": 1,
"customerID": 2,
"ec2.instancename": "server.com",
"ec2.instanceID": "i2",
"ec2.instanceType": "x5large",
"ec2.instanceAvailabilityZone": "us-east-2c",
"ec2.instanceState": "running",
"ec2.publicdns": ".com",
"ec2.publicip": "125.5.5.5",
"ec2.startdate": "11\/1\/15 1:00am",
"ec2.secgroup": "all"
}]
}
Now just trying to figure out the results and why it's not populating.... I am having no luck, is it because the JSON isnt properly formatted? Or am I not setting the scope correct?
Any help much appreciated!
Do $scope.servers = JSON.parse(response.data).ec2instance, then $scope.servers should be an array containing your servers.
Assuming the parsed data is ok (try console.log($scope.servers) after the JSON.parse line), use the following notation in your ng-repeat instead:
<tr ng-repeat="s in servers">
<td>{{ $index + 1 }}</td>
<td>{{ s.customerID }}</td>
<td>{{ s.instancename }}</td>
<td>{{ s['ec2.instanceID'] }}</td>
<td>{{ s['ec2.instanceType'] }}</td>
<td>{{ s['ec2.instanceAvailabilityZone'] }}</td>
<td>{{ s['ec2.instanceState'] }}</td>
<td>{{ s['ec2.publicdns'] }}</td>
<td>{{ s['ec2.publicip'] }}</td>
<td>{{ s['ec2.startdate'] }}</td>
<td>{{ s['ec2.secgroup'] }}</td>
</tr>

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories