I am trying to generate an invoice pdf with dynamic data (mostly taken from a form). This is the code:
class PDFController extends Controller
{
public function invoice() {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form();
return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
}
public function institution() {
$institution = Institution::where('id', 1)->get()->first();
return $institution;
}
public function user() {
$user = Auth::user();
return $user;
}
public function invoice_form(Request $request) {
$this->validate($request, array(
'furnizor-select' => 'required',
'document-number' => 'required',
'document-date' => 'required',
'due-date' => 'required',
'discount-procent' => 'required',
'discount-value' => 'required',
'total-value' => 'required',
'nir-number' => 'nullable'
));
$invoice = new \App\Models\Invoice();
$invoice->provider_id = $request->input('furnizor-select');
$invoice->number = $request->input('document-number');
$invoice->document_date = $request->input('document-date');
$invoice->due_date = $request->input('due-date');
$invoice->discount_procent = $request->input('discount-procent');
$invoice->discount_value = $request->input('discount-value');
$invoice->total = $request->input('total-value');
$invoice->save();
$invoices = Invoice::all();
$invoice_id = $invoices->last()->id;
$old_date = $request->input('document-date');
$new_date = date("d-m-Y", strtotime($old_date));
$provider_id = $request->input('furnizor-select');
$provider = Provider::where('id', $provider_id)->get();
$invoice_number = $request->input('document-number');
$old_due_date = $request->input('due-date');
$new_due_date = date("d-m-Y", strtotime($old_due_date));
$filename = 'pdfs/nir'.$invoice_id.'.pdf';
}
}
However, I am getting this error:
Too few arguments to function App\Http\Controllers\PDFController::invoice_form(), 0 passed in /Users/cristimamota/NEWSAJ BACKUP PRE FINAL/app/Http/Controllers/PDFController.php on line 17 and exactly 1 expected
And this is because in the invoice() function, I am calling the invoice_form() function and is getting no parameter.. I think this is not the correct way to do it. How should I approach it?
Since your invoice_form(Request $request) requires a Request model as parameter you can change your invoice() method to take $request as parameter and pass that $request to your invoice_form() method. Change
public function invoice() {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form();
return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
}
to
public function invoice(Request $request) {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form($request);
return view('pdf-generation.invoice')->with([
'institution' => $institution,
'user' => $user
]);
}
I am trying to generate a pdf with DOMPDF. This is my code:
class PDFController extends Controller
{
public function invoice(Request $request) {
// $institution = $this->institution();
// $user = $this->user();
$invoice = array($this->invoice_form($request));
$pdf = PDF::loadView('pdf-generation.invoice', $invoice);
return $pdf->setPaper('a4', 'landscape')->download('invoice.pdf');
//return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user, 'invoice' => $invoice]);
}
public function institution() {
$institution = Institution::where('id', 1)->get()->first();
return $institution;
}
public function user() {
$user = Auth::user();
return $user;
}
public function invoice_form(Request $request) {
$this->validate($request, array(
'furnizor-select' => 'required',
'document-number' => 'required',
'document-date' => 'required',
'due-date' => 'required',
'discount-procent' => 'required',
'discount-value' => 'required',
'total-value' => 'required',
'nir-number' => 'nullable'
));
$invoice = new \App\Models\Invoice();
$invoice->provider_id = $request->input('furnizor-select');
$invoice->number = $request->input('document-number');
$invoice->document_date = $request->input('document-date');
$invoice->due_date = $request->input('due-date');
$invoice->discount_procent = $request->input('discount-procent');
$invoice->discount_value = $request->input('discount-value');
$invoice->total = $request->input('total-value');
$invoice->save();
$invoices = Invoice::all();
$invoice_id = $invoices->last()->id;
$old_date = $request->input('document-date');
$new_date = date("d-m-Y", strtotime($old_date));
$provider_id = $request->input('furnizor-select');
$provider = Provider::where('id', $provider_id)->get();
$invoice_number = $request->input('document-number');
$old_due_date = $request->input('due-date');
$new_due_date = date("d-m-Y", strtotime($old_due_date));
$filename = 'pdfs/nir'.$invoice_id.'.pdf';
$institution = $this->institution();
$user = $this->user();
$array = array(
'invoice_id' => $invoice_id,
'new_date' => $new_date,
'provider' => $provider,
'invoice_number' => $invoice_number,
'due_date' => $new_due_date,
'provider' => $provider,
'institution' => $institution,
'user' => $user
);
return (object) $array;
}
}
And in my pdf-generation.invoice view, I have some html generate but it is not worth to post it all, so I am going to post only one line to give you some idea about the problem:
<span style="font-weight: bold; float: left;">{{$invoice->institution}}</span>
However, it says Undefined variable $invoice.. what could be the problem?
You can compact your variable like this:
$pdf = PDF::loadView('pdf-generation.invoice', compact('invoice'));
I currently use Laravel 8 and I want to know how do I ignore unique validation when a user is updating their profile? If the user is updating every field except the pageName I don't want it to throw a vaildation error cause the user is already the owner of that pageName. I tried this code but it gives error: ErrorException
Undefined variable: user
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name'.$user->id,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Auth;
use DB;
use App\Models\User;
use App\Models\Button;
use App\Models\Link;
class UserController extends Controller
{
//Statistics of the number of clicks and links
public function index()
{
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$links = Link::where('user_id', $userId)->select('link')->count();
$clicks = Link::where('user_id', $userId)->sum('click_number');
return view('studio/index', ['littlelink_name' => $littlelink_name, 'links' => $links, 'clicks' => $clicks]);
}
//Show littlelink page. example => http://127.0.0.1:8000/+admin
public function littlelink(request $request)
{
$littlelink_name = $request->littlelink;
$id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id');
if (empty($id)) {
return abort(404);
}
$information = User::select('littlelink_name', 'littlelink_color', 'littlelink_fontcolor', 'littlelink_pixiv', 'littlelink_description')->where('id', $id)->get();
$links = DB::table('links')->join('buttons', 'buttons.id', '=', 'links.button_id')->select('links.link', 'links.id', 'buttons.name')->where('user_id', $id)->orderBy('up_link', 'asc')->get();
return view('littlelink', ['information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]);
}
//Show buttons for add link
public function showButtons()
{
$data['buttons'] = Button::select('name')->get();
return view('studio/add-link', $data);
}
//Save add link
public function addLink(request $request)
{
$request->validate([
'link' => 'required|url',
'button' => 'required'
]);
$link = $request->link;
$button = $request->button;
$userId = Auth::user()->id;
$buttonId = Button::select('id')->where('name' , $button)->value('id');
$links = new Link;
$links->link = $link;
$links->user_id = $userId;
$links->button_id = $buttonId;
$links->save();
return back()->with('message', 'Link Added');
}
//Count the number of clicks and redirect to link
public function clickNumber(request $request)
{
$link = $request->link;
$linkId = $request->id;
if(empty($link && $linkId))
{
return abort(404);
}
Link::where('id', $linkId)->increment('click_number', 1);
return redirect()->away($link);
}
//Show link, click number, up link in links page
public function showLinks()
{
$userId = Auth::user()->id;
$data['links'] = Link::select('id', 'link', 'click_number', 'up_link')->where('user_id', $userId)->orderBy('created_at', 'desc')->paginate(10);
return view('studio/links', $data);
}
//Delete link
public function deleteLink(request $request)
{
$linkId = $request->id;
Link::where('id', $linkId)->delete();
return back();
}
//Raise link on the littlelink page
public function upLink(request $request)
{
$linkId = $request->id;
$upLink = $request->up;
if($upLink == 'yes'){
$up = 'no';
}elseif($upLink == 'no'){
$up = 'yes';
}
Link::where('id', $linkId)->update(['up_link' => $up]);
return back();
}
//Show link to edit
public function showLink(request $request)
{
$linkId = $request->id;
$link = Link::where('id', $linkId)->value('link');
$buttons = Button::select('name')->get();
return view('studio/edit-link', ['buttons' => $buttons, 'link' => $link, 'id' => $linkId]);
}
//Save edit link
public function editLink(request $request)
{
$request->validate([
'link' => 'required|url',
'button' => 'required',
]);
$link = $request->link;
$button = $request->button;
$linkId = $request->id;
$buttonId = Button::select('id')->where('name' , $button)->value('id');
Link::where('id', $linkId)->update(['link' => $link, 'button_id' => $buttonId]);
return redirect('/studio/links');
}
//Show littlelinke page for edit
public function showPage(request $request)
{
$userId = Auth::user()->id;
$data['pages'] = User::where('id', $userId)->select('littlelink_name', 'littlelink_color', 'littlelink_fontcolor', 'littlelink_pixiv', 'littlelink_description')->get();
return view('/studio/page', $data);
}
//Save littlelink page (name, description, logo)
public function editPage(request $request)
{
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name'.$user->id,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$profilePhoto = $request->file('image');
$pageName = $request->pageName;
$pageColor = $request->pageColor;
$pageFontcolor = $request->pageFontcolor;
$pageDescription = $request->pageDescription;
$pagePixiv = $request->pagePixiv;
User::where('id', $userId)->update(['littlelink_name' => $pageName, 'littlelink_color' => $pageColor, 'littlelink_fontcolor' => $pageFontcolor, 'littlelink_pixiv' => $pagePixiv, 'littlelink_description' => $pageDescription]);
if(!empty($profilePhoto)){
$profilePhoto->move(public_path('/img'), $littlelink_name . ".png");
}
return back()->with('message', 'Saved');
}
//Show user (name, email, password)
public function showProfile()
{
$userId = Auth::user()->id;
$data['profile'] = User::where('id', $userId)->select('name', 'email')->get();
return view('/studio/profile', $data);
}
//Save user (name, email, password)
public function editProfile(request $request)
{
$request->validate([
'name' => 'required|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
]);
$userId = Auth::user()->id;
$name = $request->name;
$email = $request->email;
$password = Hash::make($request->password);
User::where('id', $userId)->update(['name' => $name, 'email' => $email, 'password' => $password]);
return back();
}
}
Anyone know how to fix this?
You can change your code to the following
$userId = Auth::user()->id;
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name,'.$userId,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
you can use the Rule object to make that validation:
'pageName' => ['nullable','alpha_dash', Rule::unique('users','littlelink_name')->ignore($user->id)
i have created a module for creating a Quick order so customer can place there order without registering or login. i am getting 5 fields from customers to process their order and its working fine. i have debug the code below and its working fine except
$order_id = $cartManagementInterface->placeOrder($cart->getId()); this line of code is getting error of A server error stopped your order from being placed. Please try to place your order again. i dont know the issue right now and looking for quick solution. when i try to check the log i found the following error {"0":"A server error stopped your order from being placed. Please try to place your order again.","1":"#1 Magento\Quote\Model\QuoteManagement\Interceptor->___callParent('placeOrder', array(11804, NULL)) called at [vendor/magento/framework/Interception/Interceptor.php:138]\n#2 Magento\Quote\Model\QuoteManagement\Interceptor->Magento\Framework\Interception\{closure}(11804, NULL) called at [vendor/magento/module-braintree/Plugin/OrderCancellation.php:63]\n#3 Magento\Braintree\Plugin\OrderCancellation->aroundPlaceOrder(&Magento\Quote\Model\QuoteManagement\Interceptor#0000000068771282000000001567ed28#, &Closure#0000000068771726000000001567ed28#, 11804) called at [vendor/magento/framework/Interception/Interceptor.php:135]\n#4 Magento\Quote\Model\QuoteManagement\Interceptor->Magento\Framework\Interception\{closure}(11804) called at [vendor/magento/framework/Interception/Interceptor.php:153]\n#5 Magento\Quote\Model\QuoteManagement\Interceptor->___callPlugins('placeOrder', array(11804), NULL) called at [generated/code/Magento/Quote/Model/QuoteManagement/Interceptor.php:26]
namespace QuickOrder\QuickBuy\Controller\Index;
use Magento\Framework\App\Bootstrap;
class CustomOrders extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_helper;
//protected $_data;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\QuickOrder\QuickBuy\Helper\Data $helper)
{
$this->_pageFactory = $pageFactory;
$this->_helper = $helper;
return parent::__construct($context);
}
public function execute()
{
$email = "demo#demo.com";
$fname = "fname";
$lname = "lname";
$password = "password";
$product_id = 1036;
$object = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $object->get('Magento\Store\Model\StoreManagerInterface');
$customerFactory = $object->get('Magento\Customer\Model\CustomerFactory');
$cartManagementInterface = $object->get('Magento\Quote\Api\CartManagementInterface');
$cartRepositoryInterface = $object->get('Magento\Quote\Api\CartRepositoryInterface');
$customerRepository= $object->get('Magento\Customer\Api\CustomerRepositoryInterface');
$shippingRate= $object->get('Magento\Quote\Model\Quote\Address\Rate');
$productFactory=$object->get('Magento\Catalog\Model\ProductFactory');
$store = $storeManager->getStore();
$websiteId = $storeManager->getStore()->getWebsiteId();
$customer=$customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($email);
if(!$customer->getEntityId()){
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($fname)
->setLastname($lname)
->setEmail($email)
->setPassword($password);
$customer->save();
}
$cart_id = $cartManagementInterface->createEmptyCart();
$cart = $cartRepositoryInterface->get($cart_id);
$cart->setStore($store);
$customer= $customerRepository->getById($customer->getEntityId());
$cart->setCurrency();
$cart->assignCustomer($customer);
$product = $productFactory->create()->load($product_id);
$cart->addProduct($product,1);
$orderData =[
'currency_id' => 'PKR',
'email' => 'test.magecomp#gmail.com', //buyer email id
'shipping_address' =>[
'firstname' => 'John', //address Details
'lastname' => 'Deo',
'street' => 'Main Street',
'city' => 'Lahore',
'country_id' => 'PK',
'region' => 'Punjab',
'postcode' => '85001',
'telephone' => '823322565',
'fax' => '3245845623',
'shipping_method' => 'flatrate',
'save_in_address_book' => 1
],
'items'=> [
//array of product which order you want to create
['product_id'=>'1036','price'=> '899','qty'=>2]
]
];
$cart->getBillingAddress()->addData($orderData['shipping_address']);
$cart->getShippingAddress()->addData($orderData['shipping_address']);
$shippingRate->setCode('flatrate_flatrate')->getPrice(1);
$cart->getShippingAddress()->addShippingRate($shippingRate);
$cart->setPaymentMethod('cashondelivery');
$cart->setInventoryProcessed(false);
$cart->getPayment()->importData(['method' => 'cashondelivery']);
$cart->collectTotals();
$cart->save();
$cart = $cartRepositoryInterface->get($cart->getId());
$order_id = $cartManagementInterface->placeOrder($cart->getId());
return $order_id;
}
}
This is my first time to implement Paypal on my project. I am using srmklive/laravel-paypal and on the documentation I tested the following code
This is my controller:
class CheckoutController extends Controller
{
protected $provider;
public function __construct(){
$this->provider = new ExpressCheckout();
}
public function getExpressCheckout(Request $request){
$data = [];
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'qty' => 1
],
[
'name' => 'Product 2',
'price' => 4.99,
'qty' => 2
]
];
$data['invoice_id'] = 2;
$data['invoice_description'] = "test";
$data['return_url'] = url('/');
$data['cancel_url'] = url('/cart');
$total = 0;
foreach($data['items'] as $item) {
$total += $item['price']*$item['qty'];
}
$data['total'] = $total;
//give a discount of 10% of the order amount
$data['shipping_discount'] = round((10 / 100) * $total, 2);
$response = $this->provider->setExpressCheckout($data);
return redirect($response['paypal_link']);
}
}
This is my route:
Route::get('/paypal/ec-checkout', 'CheckoutController#getExpressCheckout')->name('checkout');
This is my link:
Checkout
My problem is when I click the link it just loads forever and no errors are displayed. I am using Laravel 5.6