dompdf isn't working in laravel - php

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

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]);

display the authenticated user in laravel

Hello i'm trying to display the authenticated user but i'm getting this error :
Undefined variable: users (View:
C:\wamp64\www\Blan\resources\views\users\index.blade.php)
this is my code :
usercontroller :
public function index()
{
return view('users.index')->with('user', Auth::user());
}
the view :
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
#if( $users -> count() > 0 )
#foreach ($users as $user)
<tr>
<td>
#if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
#else
No image
#endif
</td>
<td>
{{$user->name}}
</td>
<td>
Update
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
But if i return all the users using this code in the controller :
return view('users.index')->with('users', User::all());
its work fine.
so how i can return only the current authenticated user ?
You don't need to send the auth user via your controller.Auth is the global facade in laravel.Also you don't need foreach because there is only 1 authenticated user. Just type Auth::user()->name in the blade
where you want to display the user
Yea and btw the error ur getting is because you are doing foreach for $users but sending $user to blade but im pretty sure it wont work even if you correct that typo
In your example
public function index()
{
return view('users.index')->with('user', Auth::user());
}
You have used "user"
But tried to access variable as "users"
Unless you just made a typo in your example?

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
}

Can't delete from SQLite table in Laravel

I'm Doing a "take home assignment" for job interview. While I have some experience in web development, its not my forte. I am trying to delete a row in a SQLite table using a HTML DELETE button. I am using Laravel-php framework.
I've tried different solutions on google and stack Overflow, but none seem to solve the problem. I modeled my approach after this Laracasts video
Link to my code
The blade seems to be passing the correct info ($id from $contact->id) & the controller seems to be receiving. But the given contact associated with the id isn't being deleted.
FROM BLADE:
<div class="col-md-6">
<table class="table table-striped table-hover">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
#foreach($contacts as $contact)
<tr>
<td> {{$contact->f_name}} </td>
<td> {{$contact->l_name}} </td>
<td> {{$contact->address}} </td>
<td>
<form method="POST" action="/delete/{{ $contact->id }}">
#method('DELETE')
#csrf
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Contact</button>
</div>
</div>
</form>
</td>
</tr>
#endforeach
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
FROM CONTROLLER:
public function delete($id) {
Contact::find($id)->delete();
return view('index');
}
FROM ROUTE:
Route::delete('/delete', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
you're not getting the id on your delete method, you need to include it on the url like
Route::delete('/delete/{id}', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
in this case, you don't need to change the delete method.
you can also retrieve the id from the request object without changing the delete route, int this case you need to include the id as a hidden input on your view and your delete method will look like this
public function delete(Request $request) {
Contact::find($request->id)->delete();
return view('index');
}

Laravel 5.2: Can variable be classified in view?

I am using Laravel 5.2.
My question is:
Can variable be classified in view?
For example:
There is a controller ,it can get all of the articles belonging to the current user, like this:
public function index()
{
$user=\Auth::user();
$articles = $user->articles;
return view('user.dashboard.index', compact('articles'));
}
In view,these codes below can show the articles,like this:
<div class="card">
<div class="card-header">
Articles
</div>
#if ($articles!=null)
<table class="table table-sm">
<thead>
<tr>
<th>title</th>
<th>created time</th>
<th>status</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{$article->title}}</td>
<td>{{$article->created_at}}</td>
<td>{{$article->status}}</td>
</tr>
#endforeach
</tbody>
</table>
#else
<p>Nothing</p>
#endif
</div>
These articles can be classified into two types:
1、“published”,status is 1.
2、“unpublished”,status is 0.
Question:
I would want the published articles to be shown in a card(div class="card"), and, the unpublished articles to be shown in another card.
Can they be classified in view? Thus,it doesn't need to query twice.
Try to use collection method where()
#foreach ($articles->where('published', 1)->all() as $article)
Just change published and 1 to real ones.

Categories