i am building an asset management system.I would like to have the following calculations done in the controller to be displayed in the view.
public function depreciation()
{
$assets = Asset::all();
$price = DB::table('assets')
->where('category_id', 1)
->sum('purchase_price');
$dep = $price * 0.3333;
$netprice = $price - $dep;
return $netprice;
return view('admin.assets.index')->with(['price','dep', 'netprice' => $netprice]);
}
My Route
Route::post('assets_depreciation', ['uses' => 'Admin\AssetsController#depreciation', 'as' => 'assets.depreciation']);
My View
<tbody>
#if (count($assets) > 0)
#foreach ($assets as $asset)
<tr data-entry-id="{{ $asset->id }}">
#can('asset_delete')
<td></td>
#endcan
<td field-key='title'>{{ $asset->title }}</td>
<td field-key='serial_number'>{{ $asset->serial_number }}</td>
<td field-key='barcode'>{{ $asset->barcode }}</td>
<td field-key='photo1'>#if($asset->photo1)<img src="{{ asset(env('UPLOAD_PATH').'/thumb/' . $asset->photo1) }}"/>#endif</td>
<td field-key='category'>{{ $asset->category->title ?? '' }}</td>
<td field-key='status'>{{ $asset->status->title ?? '' }}</td>
<td field-key='location'>{{ $asset->location->title ?? '' }}</td>
<td field-key='assigned_user'>{{ $asset->assigned_user->name ?? '' }}</td>
<td field-key='vendor'>{{ $asset->vendor->name ?? '' }}</td>
<td field-key='purchase_price'>{{ $asset->purchase_price }}</td>
<td field-key='warranty'>{{ $asset->warranty }}</td>
<td field-key='depreciation'>{{ $netprice }}</td>
<td>
How can This be achieved?
You should be using a GET request instead of a POST for your route. It should look like this:
Route::get('assets_depreciation', ['uses' => 'Admin\AssetsController#depreciation',
'as' => 'assets.depreciation']);
Laravel's documentation gives you correct usage examples of their framework components, you can check it out here: Laravel/Routing. Hope this helps!
If you are looking to send the price, dep and netprice to the admin.assets.index view then you can use this:
public function depreciation()
{
$assets = Asset::all();
$price = DB::table('assets')
->where('category_id', 1)
->sum('purchase_price');
$dep = $price * 0.3333;
$netprice = $price - $dep;
return view('admin.assets.index')->with(['price' => $price,'dep' => $dep, 'netprice' => $netprice]);
}
Then you can use these variables in you view file {{ $price }}, {{ $dep }}, {{ $netprice }}
You need to create file index.blade.php in your_project/resources/views/admin/assets/ with html-blade content e.g:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>my page</title>
</head>
<body>
<div>Price: {{ $price }}</div>
<div>Dep: {{ $dep }}</div>
<div>Net price: {{ $netprice }}</div>
</body>
</html>
more info here. And remove first return statement and change last return to
return view('admin.assets.index', compact('price','dep','netprice'));
Related
I'm facing an issue with domPDF. pdf result getting displayed as html page not as pdf document. I'm trying to get the data from database and get the result in pdf document using 'dompdf' for laravel.
pdf-output-image
here is my HTML code.
`<!DOCTYPE html>
<html>
<head>
<title>MHT Order PDF</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body>
<div class="d-block text-center" style="text-align: center; margin-top: -15px;">
<h1 style="text-align: center" class="navbar-brand text-underline text-center"><u> MAJESTIC HOUSE </u></h1>
<p style="margin-top:-20px">General Trading LLC</p>
</div>
<div class="order_heading">
#foreach($orderDtls as $order)
<p class="date">Date: {{$date}}</p>
<h3 class="orderDtls">Order Details </h3>
<h5 class="details">Order UID: <strong style="color: red;">{{$order->order_unq_id}}</strong></h5>
<h5 class="details">Customer Name: <strong style="color: red;">{{$order->customer_name}}</strong></h5>
<p id="date"> </p>
</div>
<table class="table caption-top" id="orderTable">
<caption>Order Details</caption>
<thead>
<tr>
<th scope="col">Customer Name</th>
<th scope="col">Product Name</th>
</tr>
</thead>
<tbody>
#foreach($orderDtls as $order)
<tr>
<td scope="row">{{$order->customer_name}}</td>
<td>{{$order->item_name}}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
</body>
</html>
this is the code from Controller
public function mht_order_pdf(){
$customers = Customer::all();
$linkeds = Linked::all();
$orders = Order::all();
$date = date('y-m-d');
$orderDtls = DB::select("SELECT
customers.customer_name,
orders.id,
orders.order_unq_id,
items.item_name,
orders.item_quantity,
orders.total
FROM customers
JOIN linkeds
ON customers.id = linkeds.customer_id
JOIN items
ON linkeds.item_id = items.id
JOIN orders
ON linkeds.id = orders.linked_id
WHERE orders.order_unq_id = 'MH-Ord/29722/xyz supermarket';"
);
// // return $orderDtls;
return view('mht_order_pdf', compact('customers', 'linkeds', 'orders', 'orderDtls',
'date'));
$pdf = PDF::loadView('mht_order_pdf', $orderDtls);
return $pdf->stream('mht_order_pdf.pdf');
}
this is the code from web.php
<?php
use App\Http\Controllers\Backend\CustomerController;
use App\Http\Controllers\Backend\ItemController;
use App\Http\Controllers\Backend\LinkedController;
use App\Http\Controllers\Backend\OrderController;
use App\Http\Controllers\Backend\ProductController;
use App\Http\Controllers\Backend\UserController;
use App\Http\Controllers\PDFController;
use App\Models\Customer;
use App\Models\Linked;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Support\Facades\Route;
use Dompdf\Dompdf;
use Illuminate\Support\Facades\DB;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])-
>name('home');
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
Route::resource('items', ItemController::class);
Route::resource('customers', CustomerController::class);
Route::resource('linked', LinkedController::class);
Route::resource('orders', OrderController::class);
Route::get('mht_order_pdf', [OrderController::class, 'mht_order_pdf'])-
>name('mht_order_pdf');
PLEASE LET ME KNOW WHAT I'M DOING WRONG?
Thank you!
Your controller stops at this line:
return view('mht_order_pdf', compact('customers', 'linkeds', 'orders', 'orderDtls', 'date'));
thus the 2 lines below it, which related to the DomPDF, gets ignored. Try to delete the return view() above so that the DomPDF lines of code below it can be executed.
I am going to create a pdf file from HTML using the Barryvdh Laravel package. I need to add a header and footer to each page as this is a multi-page document.
I've looked around a bit and I think that the two links below will help you solve your problem:
1- https://github.com/barryvdh/laravel-snappy/issues/139#issuecomment-369050826
Create a route that renders the html for the header/footer, and pass the full url to that route as option.
$html = view()->make("juiztramp.exporter.pdf.grupos")
->with("provas",$ordens_de_passagem)
->render();
$headerHtml = view()->make('juiztramp.exporter.pdf.header')
->with('nomeEvento', $nomeEvento)
->with('dataEvento', $dataEvento)
->with('localEvento', $localEvento)
->render();
$footerHtml = view()->make('juiztramp.exporter.pdf.footer')
->with('organizacao', $organizacao)
->render();
And later on
$options = [
'orientation' => 'portrait',
'encoding' => 'UTF-8',
'header-html' => $headerHtml,
'footer-html' => $footerHtml,
// further options....
];
return response(
$snappy->getOutputFromHtml($html, $options),
200,
[
'Content-Type' => 'application/pdf',
// 'Content-Disposition' => 'attachment; filename="'.$filename.'"',
'Content-Disposition' => 'filename="'.$filename.'"',
]
);
example header view
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<table>
<tbody>
<tr>
<td style="width: 200px; background-color: #00be67;"> {{ $nomeEvento }} </td>
<td style="width: 500px; background-color: #0a94e3;" align="center"> {{ $localEvento }} </td>
<td style="width: 200px; background-color: #7b3f25" align="right"> {{ $dataEvento }} </td>
</tr>
</tbody>
</table>
</body>
</html>an
So guys I add pagination to my project and I'm almost finishing it, witch by the way is my first project ever, and all I need to do right now is to set pagination by created_at.So I need to put posts from the same day on same page link. Right now it shows me posts from different days on one page. And after that I just need to show price sum of that day. If you know please help me, this would be my first project as a student, I'm still learning. Thank you !!
Here is my HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Post;
use DB;
use Auth;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$posts = Post::where('user_id', Auth::id())->orderBy('created_at', 'DESC')
->paginate(3);
return view('home')->with('posts', $posts);
}
}
Here is my home.blade.php
#extends('layouts.theme')
#section('content')
<style>
body{
margin: 0;
background-color: #EBEBEB;
}
</style>
<div class="mainl">
<div class="row">
<div class="columna">
<h1>{{ Auth::user()->name }}</h1>
<hr>
</div>
<div class="columns">
<a href="{{ route('logout') }}" id="logout"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('LOGOUT') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</div>
</br></br></br>
#if(count($posts)> 0)
<table>
<thead>
<tr>
<th>BR.KESICE</th>
<th>IME I PREZIME</th>
<th>BR.TELEFONA</th>
<th>POSAO</th>
<th>CIJENA</th>
<th>PLACANJE</th>
<th>POPUST</th>
<th>DATUM PREUZ.</th>
<th>DATUM IZDAV.</th>
<th>SMJENA</th>
<th>RADNIK</th>
<th>STATUS</th>
<th>IZMIJENI</th>
</tr>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{$post->br_kesice}}</td>
<td>{{$post->ime}}</td>
<td>{{$post->br_telefona}}</td>
<td>{{$post->posao}}</td>
<td>{{$post->cijena}}</td>
<td>{{$post->placanje}}</td>
<td>{{$post->popust}}</td>
<td>{{$post->datum_preuz}}</td>
#if($post->status == 1)
<td>/</td>
#else
<td>{{$post->datum_izdav}}</td>
#endif
<td>{{$post->smjena}}</td>
<td>{{$post->radnik}}</td>
<td>
#if($post->status == 0)
<span class="label label-primary" id="statusdeaktivan">Deaktivan</span>
#elseif($post->status == 1)
<span class="label label-success" id="statusaktivan">Aktivan</span>
#elseif($post->status == 2)
<span class="label label-danger" id="statusdeaktivan">Rejected</span>
#else
<span class="label label-info" id="statusdeaktivan">Deaktivan</span>
#endif
</td>
#if($post->status == 3)
#else
<td><i class="far fa-edit"></i></td>
#endif
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->sum('cijena')}}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->sum('cijena')}}€</th>
</tr>
</tfoot>
</table>
{{ $posts->links()}}
#else
<p>Trenutno nema unosa.</p>
#endif
</div>
#endsection
And I found something from past questions here in stack overflow and it's working but I have a problem with that, so
public function index()
{
$posts = Post::where('user_id', Auth::id())->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->get();
return view('home')->with('posts', $posts);
}
When I add this ->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->get(); it shows me all posts from past 10 days but after that 10 days nothing, could I use this but to sort it by all days, not to set maximum days?
I also add this code to my price sum and it is working but as I said just for past 10 days
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
</tr>
</tfoot>
Thank you again and I would really appreciate it if you help me with this. It's my first project and I'm very happy about it.
What your current code is doing is taking all the posts, sort it by created_at and then show 3 at a time. It doesn't care about what day it is or how many $post are there for that particular day.
What I understand that, you want to do is to show it by date. In that case, your query should filter based on date.
What you need to do is pass the date to the controller and fetch only posts from that day. It is a bit tricky. You need to pass today's date to controller.
In HTTP GET method your URL should contain the date. For example, it can be something like http://example.com/index?date=20190716. Notice that "date=20190716" part. You can get this in controller. Then you can get the posts of that day by writing,
$date = new Carbon(request('date'));
$posts = Post::where('user_id', Auth::id())
->whereDate('created_at','=',$date)
->orderBy('created_at', 'DESC')
->paginate(3);
Now, what it will do is get only the posts from 16th July, 2019.
This is the main idea. How you pass the date is entirely on you. You can do that in several ways. You can use JS, HTTP GET method and HTTP POST method.
Try
public function index()
{
$posts = Post::where('user_id', Auth::id())->get();
return view('home')->with('posts', $posts);
}
Try this also, I hope helps you
public function index()
{
$dateFromUrl = \Carbon\Carbon::createFromFormat('d/m/Y', request()->date);
$posts = Post::where('user_id', Auth::id())->where('created_at',$dateFromUrl)->orderBy('created_at', 'DESC')
->get();
$sum = 0;
foreach($posts as $post){
$sum+=$post->price;
}
return view('home', compact("posts","sum"));
}
I have a serious problem,
I want to show the Hebrew words in RTL format after PDF creation, but it's not showing. It always shows LTR.
I have some words combinations of English and Hebrew language.
I did some search on google but no luck.
I am using Laravel DomPdf.
I have checked my dompdf core file DOMPDF_UNICODE_ENABLED and its value is "DOMPDF_UNICODE_ENABLED" => true, still not abel to get the solution.
Here is my blade file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl">
</head>
<body>
<table class="lead" cellspacing="0" width="100%" style="font-size: 12px; font-family: 'firefly, DejaVu Sans, sans-serif'; ">
<thead>
<tr>
<th width="10%" style="font-weight: bold;">Date</th>
<th width="15%" style="font-weight: bold;">Name</th>
<th width="20%" style="font-weight: bold;">Details</th>
<th width="10%" style="font-weight: bold;">Contact Origin</th>
<th width="15%" style="font-weight: bold;">Status</th>
<th width="10%" style="font-weight: bold;">Comment</th>
<th width="10%" style="font-weight: bold;">Country</th>
</tr>
</thead>
<tbody>
<?php
foreach($leadInfo as $k=>$v){
$continent = getAllContinentName($v->country);
if(strtolower($continent) == strtolower("Asia")){
$backGround = "#FFB300";
} else if(strtolower($continent) == strtolower("Africa")){
$backGround = "#FFB300";
} else if(strtolower($continent) == strtolower("North America")){
$backGround = "#009792";
} else if(strtolower($continent) == strtolower("South America")){
$backGround = "#FF7E00";
} else if(strtolower($continent) == strtolower("Antarctica")){
$backGround = "#15E6E8";
} else if(strtolower($continent) == strtolower("Europe")){
$backGround = "#0074FF";
} else if(strtolower($continent) == strtolower("Australia")){
$backGround = "#05A900";
} else {
$backGround = "#FFFFFF";
}
if($v->email_status_id == 10){
$statusBackGround = "#a9d18d";
} else if($v->email_status_id == 11){
$statusBackGround = "#ff0000";
} else if($v->email_status_id == 12){
$statusBackGround = "#b3c6e7";
} else if($v->email_status_id == 13){
$statusBackGround = "#c09200";
} else if($v->email_status_id == 14){
$statusBackGround = "#ffff00";
} else {
$statusBackGround = "#FFFFFF";
}
?>
<tr>
<td> {{ date('m-d-Y',strtotime($v->created_date)) }} </td>
<td>{{ $v->name }}</td>
<td>{{ mb_substr($v->message, 0, 300) }}</td> <!-- This line has combination of english and hebrew language-->
<td>{{ $v->contact_origin }}</td>
<td style="direction: rtl !important; unicode-bidi: bidi-override; color:black; background-color: {{ $statusBackGround }};">{{ $v->status_name }}</td> <!-- This line has only hebrew language -->
<td>{{ $v->comment }}</td>
<td style="color:black; background-color: {{ $backGround }}" >{{ getAllCountryName($v->country) }}</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
It would be great if anyone helps me to get out of this.
Dompdf (up to and including 0.8.1) does not currently support RTL text (see issue 1009). There is a work around, but the results are passable at best.
If you're interested in trying it out modify the Text rendered by adding the following code at line 83:
if (strtolower($style->direction) === 'rtl') {
preg_match_all('/./us', $text, $ar);
$text = join('',array_reverse($ar[0]));
// if there are numbers in the string so the next line reverse the number back treat also numbers with dot (decimal) and email
$text = preg_replace_callback('/\d+-\d+|\d+|\d+\.\d+|\S+#\S+/', function (array $m) { return strrev($m[0]); }, $text);
}
you can use this package which supports rtl languages such as persian and arabic
https://github.com/barryvdh/laravel-snappy
in order to change the direction , just within the html tag which is going to be converted to pdf , use dir="rtl"
In my ImageController, I've moved an image to a directory and saved the image path in a table in database as,
public function store(Request $request)
{
$new_country = new SelectCountry();
$message = [
'required' => "This field can not be empty",
];
$this->validate($request, [
'country_name' => 'required',
'alternate_title' => 'required',
'country_flag' => 'required',
], $message);
$new_country->country_name = $request->country_name;
$new_country->alternate_title = $request->alternate_title;
if($request->hasfile('country_flag'))
{
$country_flag_image = $request->file('country_flag');
$country_flag_name = $country_flag_image->getClientOriginalName();
$extention = $country_flag_image->getClientOriginalExtension();
$dirpath = public_path('assets/images/country/');
$country_flag_image->move($dirpath,$country_flag_name);
$new_country->country_flag_path = $dirpath.$country_flag_name;
}
$new_country->save();
return redirect()->route('admin.country');
}
Now I want to use the path and display the image along other informations. I've used the following code to return view,
public function select_country()
{
$countries = SelectCountry::all();
return view('auth.select_country')->with('countries',$countries);
}
And in the view, I've used following code to display datas stored in the database,
<table class="table-hover table-responsive container-fluid col-xs-12 col-sm-12">
<thead>
<tr>
<th>Country Name</th>
<th>Alternate Title</th>
<th>Country Flag</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($countries as $country)
<td>{{ $country->country_name }}</td>
<td>{{ $country->alternate_title }}</td>
<td><img src="{{ $country->country_flag_path }}"></td>
<td>
<a type="button" href="" class="btn btn-warning btn-xs">Update</a>
<a type="button" href="" class="btn btn-danger btn-xs">Delete</a>
</td>
#endforeach
</tbody>
The problem is the image is not displayed while other informations are displayed. The src attribute in img tag is returning path but not directory.
If there is any solution, please tell me. Thanks.
I think your issue lies with where you are setting the storage folder. Laravel with automatically reference the public folder for storage of images etc so there's no need for the public_path function.
Try setting your $dirpath to just $dirpath = "/asset/images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ $country->country_flag_path }}">
Then upload a new image and see what the database has the path set to, hopefully it should be a bit more normal :)
Please note that my original answer of:
Try setting your $dirpath to just $dirpath = "images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ asset($country->country_flag_path) }}">
Should still work as it's how I've always done it :)