Datatable - Server side data is not displaying - php

I am playing with datatable with Laravel 5 with first time so I tried with this code and I am getting data on ajax but not able to display data on my datatable.
Any Idea how to load response data to datatable.
Here is my structure:
Result:
Route:
Route::get('admin/pages/datatable', 'admin\PagesController#getDataTable'); // Pages (Static Pages)
pages.blade.php:
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Pages Table</h3>
</div><!-- /.box-header -->
<div class="box-body">
<table id="example32" class="table table-bordered table-hover">
<thead>
<tr>
<th>PageId</th>
<th>Title</th>
<th>Text</th>
<th>MetaKeywords</th>
<th>MetaDescription</th>
<th>PostedBy</th>
</tr>
</thead>
<tfoot>
<tr>
<th>PageId</th>
<th>Title</th>
<th>Text</th>
<th>MetaKeywords</th>
<th>MetaDescription</th>
<th>PostedBy</th>
</tr>
</tfoot>
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div><!-- /.col -->
</div><!-- /.row -->
</section><!-- /.content -->
PagesController.php:
public function getDataTable()
{
$Pages = new Pages();
return $GetAllPages = $Pages->GetPages();
}
Pages.php:
public function GetPages()
{
return DB::table('pages')->select('PageId', 'Title', 'Text', 'MetaKeywords', 'MetaDescription', 'PostedBy')
->get();
}

You have to pass an indexed array instead of associative. Your response should be like
{['pageid', 'Title', 'Text', ... ]}
Just need to follow the correct column order.
May be you can use,
public function GetPages()
{
$pages = DB::table('pages')->select('PageId', 'Title', 'Text', 'MetaKeywords', 'MetaDescription', 'PostedBy')->get();
$pages = $pages->map(function($item){
return [$item->PageId, $item->Title,$item->Text,$item->MetaKeywords,$item->MetaDescription, $item->PostedBy ];
});
return $pages;
}

Related

Get the the average of the data in laravel

I have two eloquent tables, vendor and work, in the work table there is a total score column and I want to get the average of the total score based on the vendor ID (foreign Key) and save it to the vendor table. May I know how to achieve this?
Here is my vendor table blade file
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Penilaian Penyedia</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpenyedia" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Nama Penyedia</th>
<th>Nilai</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#foreach ($penyedia as $penyedias)
<tr>
<td>{{$no++}}</td>
<td>{{$penyedias->nama}}</td>
<td></td>
<td>
Beri Penilaian
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
Here is the AdminController
public function update_nilaipekerjaan(Request $request, $id){
$pekerjaan = Pekerjaan::find($id);
//$pekerjaan->update($request->all());
//$total = Pekerjaan::select(DB::raw('avg(nilai_1 + nilai_2 + nilai_3 + nilai_4) as nilai_total'))->get();
$pekerjaan->nilai_1=$request->nilai_1;
$pekerjaan->nilai_2=$request->nilai_2;
$pekerjaan->nilai_3=$request->nilai_3;
$pekerjaan->nilai_4=$request->nilai_4;
$pekerjaan->nilai_total=$request->nilai_1+$request->nilai_2+$request->nilai_3+$request->nilai_4;
$pekerjaan->update();
return redirect()->back()->with('message','Nilai Pekerjaan Berhasil diupdate!');
}
public function tabelnilai_penyedia(){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
} //I WANT TO SHOW THE AVERAGE OF THE TOTAL SCORE HERE.
Database https://imgur.com/a/N6NlcwJ
Here is what I want to achieve https://imgur.com/a/fk4Yq92
what I have tried
updating the AdminController
public function tabelnilai_penyedia($id){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
$average = Pekerjaan::where('penyedia_id', $id)->avg('nilai_total');
Penyedia::where('id', $id)->update(['nilai'=>$average]);
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
}
But it got me an error,
Too few arguments to function App\Http\Controllers\AdminController::tabelnilai_penyedia(), 0 passed in C:\Users\ASUS TUF\webpenyedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
You can use the MySQL AVG function to get the average of a column, which in Eloquent is neatly wrapped in an aggregate function
Assuming your models are called "Work" and "Vendor"
// Get average of 'nilai' based on Vendor ID
$average = Work::where('penyedia_id', $vendorId) // Assuming this is the right column
->avg('nilai_total');
// Update Vendor directly in database (No model events)
Vendor::where('id', $vendorId)->update(['nilai' => $average]);

I am getting a "Trying to get property of non-object" error from my controller

I am getting a "Trying to get property of non-object" error whenever I try to process a deposit in my controller
Here is my controller
users::where('id',$user->id)
->update([
'confirmed_plan' => $deposit->plan,
'activated_at' => \Carbon\Carbon::now(),
'last_growth' => \Carbon\Carbon::now(),
]);
//get plan
$p=plans::where('id',$deposit->plan)->first();
//get settings
$settings=settings::where('id', '=', '1')->first();
$earnings=$settings->referral_commission*$p->price/100;
//increment the user's referee total clients activated by 1
agents::where('agent',$user->ref_by)->increment('total_activated', 1);
agents::where('agent',$user->ref_by)->increment('earnings', $earnings);
}
//update deposits
deposits::where('id',$id)
->update([
'status' => 'Processed',
]);
return redirect()->back()
->with('message', 'Action Sucessful!');
}
And The Error seems to be at this line of code
$earnings=$settings->referral_commission*$p->price/100;
And Here is my process deposit view blade
#include('header')
<!-- //header-ends -->
<!-- main content start-->
<div id="page-wrapper">
<div class="main-page signup-page">
<h3 class="title1">Manage clients deposits</h3>
#if(Session::has('message'))
<div class="row">
<div class="col-lg-12">
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<i class="fa fa-info-circle"></i> {{ Session::get('message') }}
</div>
</div>
</div>
#endif
<div class="bs-example widget-shadow table-responsive" data-example-id="hoverable-table">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Client name</th>
<th>Client email</th>
<th>Amount</th>
<th>Payment mode</th>
<th>Plan</th>
<th>Status</th>
<th>Date created</th>
<th>Option</th>
</tr>
</thead>
<tbody>
#foreach($deposits as $deposit)
<tr>
<th scope="row">{{$deposit->id}}</th>
<td>{{$deposit->duser->name}}</td>
<td>{{$deposit->duser->email}}</td>
<td>${{$deposit->amount}}</td>
<td>{{$deposit->payment_mode}}</td>
#if(isset($deposit->dplan->name))
<td>{{$deposit->dplan->name}}</td>
#else
<td>For withdrawal</td>
#endif
<td>{{$deposit->status}}</td>
<td>{{$deposit->created_at}}</td>
<td> <a class="btn btn-default" href="{{ url('dashboard/pdeposit') }}/{{$deposit->id}}">Process</a></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#include('modals')
#include('footer')
I keep getting "Trying to get property of non-object" error without any explanation as to where to look at. I need help please.
Are you sure you have a $settings object with id of 1?
$settings=settings::where('id', '=', '1')->first(); // <-- why 1, why not just first()?
If you are hard coding a single id for a single setting into the DB that will always be the same, and always id of 1... consider just adding that to code instead.
Also check on $deposit->plan -- is this correct, or maybe should this be $deposit->plan_id or similar? Perhaps the $p is null because there is no value for $deposit->plan.
You can set some error checking here by checking for null ahead of the calculation:
if(isset($settings) && isset($p)){
$earnings=$settings->referral_commission*$p->price/100;
}
else
{
$earnings = 0; // or whatever you want to replace it with should there be no setting
}

Get Entity's ID in Datastore PHP API

Is there a way to get the entity's ID/Key using PHP Datastore API?
I've already authenticated everything and I'm able to get the values, but no the ID
Here's my code:
<?php
$title = "Builder | Makeroid Account";
include "../assets/includes/head.php";
if (!$USER->is_logged_in()) {
$USER->redirect('/');
}
require_once __DIR__.'/../assets/libs/datastore/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\Query\Query;
$datastore = new DatastoreClient([
'projectId' => 'makeroid-builder'
]);
$query = $datastore->query();
$query->kind('UserData');
$query->filter('emaillower', '=', $U_DATA['email']);
$users = $datastore->runQuery($query);
$params = ["email", "emailFrequency", "emaillower", "isAdmin", "link", "name", "sessionid", "settings", "templatePath", "tosAccepted", "type", "upgradedGCS"];
?>
<div class="content">
<div class="container-fluid">
<div class="row">
<?php foreach ($users as $user) { ?>
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-icon" data-background-color="rose">
<i class="material-icons">assignment</i>
</div>
<div class="card-content">
<h4 class="card-title">User Properties</h4>
<div class="table-responsive">
<table class="table">
<thead class="text-primary">
<th>Key</th>
<th>Value</th>
</thead>
<tbody>
<?php print_r($user); foreach ($params as $param) { ?>
<tr>
<td><?=$param?></td>
<td><?=$user?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
<?php include "../assets/includes/footer.php"; ?>
I would like to get this:
I'm able to get all values that are in the rows, such us my email, but I hadn't found a way to get that ID
I've tried by using $user['id'], $user['key'] or $user['__key__'] but none of them worked
You'll need to pull the key from the entity (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/entity?method=key) then pull the id from the key (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/key) with something like $key->pathEndIdentifier().
So instead of trying to pull the key out of object as a dictionary value, try pulling it out as a method. $user->key()->pathEndIdentifier() .

dompdf isn't working in laravel

I have make a html view page for generate to a pdf report from this view and i have also installed dompdf library, i have loaded data from database table on this view and create a hyperlink name as Download pdf. but when i click on this link it's show undefined variable dsale error.
here my controller method.
//pass data to view
public function dsreport($id,$fromdate,$todate)
{
$dsale=DB::table('directsales')
->join('clients','directsales.client_id','=','clients.id')
->join('products','directsales.product_id','=','products.id')
->select('clients.client_name','clients.addr','directsales.*','products.name')
->where('directsales.client_id','=',$id)
->whereBetween('directsales.issue_date',[$fromdate,$todate])
->distinct()
->paginate(3);
//->get();
$sumt=DB::table('directsales')
->where('directsales.client_id','=',$id)
->sum('directsales.total');
return view('reports.directsalereport',compact(['dsale','sumt']));
}
//generate pdf
public function dsalepdf()
{
$pdf=PDF::loadView('reports.directsalereport');
return $pdf->download('dsread.pdf');
}
Here is my view part.
<div class="panel-body">
<center><h1><strong>M/s. Priti Enterprise</strong></h1></center>
</center>
<center><h2><i>Daily Sales Report</i></h2></center>
<div class="row">
<div class="col-md-4"><strong>Client Name: {{$dsale[0]->client_name}}</strong></div>
<div class="col-md-4"></div>
<div class="col-md-4"><strong>Delivered by: {{$dsale[0]->deliverd_by}}</strong></div>
</div><br>
<div class="row">
<div class="col-md-4"><strong>Client address: {{$dsale[0]->addr}}</strong></div>
<div class="col-md-4"></div>
<div class="col-md-4"><strong>Date: {{$dsale[0]->issue_date}}</strong></div>
</div><br><br>
<table class="table table-bordered">
<thead>
<th class="col-md-1">SL no.</th>
<th>Product Name</th>
<th>Invoice no.</th>
<th>Unit per ctn.</th>
<th>Unit price</th>
<th class="col-md-2">Sales</th>
<th>Value</th>
</thead>
<tbody>
#foreach($dsale as $d)
<tr>
<td>#</td>
<td>{{$d->name}}</td>
<td>{{$d->transaction_code}}</td>
<td>{{$d->unitperctn}}</td>
<td>{{$d->unitprice}}</td>
<td>{{$d->ctn}} ctn {{$d->pcs}} pcs</td>
<td>{{$d->total}}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="col-md-11"></div>
<div class="row"><strong>Total:{{$sumt}}</strong></div>
<div>{{$dsale->links()}}</div>
<div class="col-md-8"></div>
<div class="row">
<i class=" glyphicon glyphicon-ok"></i> Download PDF
</div>
</div>
You have an array in compact.
You should use <div>{{$dsale[0]->links()}}</div>.
I think you should dont compact an array.
return view('reports.directsalereport',compact('dsale','sumt'));
Edited:
You should pass data to your Pdf view, too, so your method for downloading pdf should look like this.
public function dsalepdf()
{
$dsale=DB::table('directsales')
->join('clients','directsales.client_id','=','clients.id')
->join('products','directsales.product_id','=','products.id')
->select('clients.client_name','clients.addr','directsales.*','products.name')
->where('directsales.client_id','=',$id)
->whereBetween('directsales.issue_date',[$fromdate,$todate])
->distinct()
->paginate(3);
$sumt=DB::table('directsales')
->where('directsales.client_id','=',$id)
->sum('directsales.total');
$pdf=PDF::loadView('reports.directsalereport',['dsale' => $dsale,'sumt'=> $sumt]);
return $pdf->download('dsread.pdf');
}
You may need to delete the vendor folder, then run composer install

Yii2 rendering in a specific div

I'm using Yii2-advanced-template. I've 2 divisions on my form as follows - When I'll click on any of the radio button in 2nd div, the details of respective entry should be loaded in 1st div. That is, the form in 1st div will remain as is, but loads some values for update operation. But I'm getting it as - which is not what I expect. It is loading the whole page layout in that div.
I already used 'renderPartial()' in my controller. But it solved only the half of the problem, that is, the navigation bar & other layout is hidden after using renderPartial().
My form is as follows-
<div class="col-sm-12">
<div class="col-sm-6 fillitemform">
...
Enter Item Details form
...
</div>
<div id="item_details" class="col-sm-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Item details</h3>
</div>
<div class="panel-body">
<div class="table-responsive" >
<table class="table">
<thead>
<tr><th></th>
<th>PO No.</th>
<th>SKU</th>
<th>Order Type</th>
<th>Expected Qty</th>
<th>Received Qty</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
<?php
for($itr = 0, $ro = $modelRO, $rod = $modelROD; $itr < count($modelROD); ++$itr){
echo '<tr onclick="placeitemdtls('.$rod[$itr]['id'].')">';
echo '<td><input type="radio" name="item"></td>';
echo '<td>'.$ro[0]['ro_po_no'].'</td>';
echo '<td>'.$rod[$itr]['rod_sku'].'</td>';
echo '<td>'.$ro[0]['ro_order_type_id'].'</td>';
echo '<td>'.$rod[$itr]['rod_expected_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_received_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_weight'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
My JS/jQuery function-
function placeitemdtls(id){
$.post("index.php?r=receiving-order-details/update&id="+id, function(response) {
jQuery(".fillitemform").html(response);
});
}
My Controller function-
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelRO = ReceivingOrders::find()->where(['id' => $id])->all();
$modelROD = ReceivingOrderDetails::find()->where(['receiving_order_id' => $id])->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['create', 'id' => $id]);
} else {
return $this->renderPartial('update', [
'model' => $model, 'modelRO' => $modelRO, 'modelROD' => $modelROD,
]);
}
}
Please note that, I'm getting the correct form with proper values, but having rendering problem as I showed in 2nd image. Please help me to solve it. So that my output should look like -

Categories