Laravel 8 Undefined Variabel - php

I'm still a beginner in Laravel, and I'm confused by this error.
Undefined variable: today (View: D:\Documents\My
Project\todo-app\resources\views\edit.blade.php)
This my error
this my controller
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required|string|max:25',
'finished' => 'nullabel'
]);
$home = Home::find($id);
$home->title = $request->input('title');
if($request->has('finished')){
$home->finished = true;
}
$home->user_id = Auth::user()->id;
$home->save();
$mytime=time::now();
$date=$mytime->toRfc850String();
$today= substr($date, 0, strrpos($date, ","));
$date = Carbon::now()->format('d-m-Y');
return redirect('/home', compact('homes'), [
'title'=>'Home',
'date' => $date,
'today' => $today
]);
}
What should I do in my controller? is calling $today correct? I'm so confused please

All things are right, but does not declare the variable for today($today)
Even the date variable is also declared. That one also you should be declared.
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required|string|max:25',
'finished' => 'nullabel'
]);
//This line added
$today = date('Y-m-d H:i:s');
$date = date('Y-m-d H:i:s');
$home = Home::find($id);
$home->title = $request->input('title');
if($request->has('finished')){
$home->finished = true;
}
$home->user_id = Auth::user()->id;
$home->save();
return redirect('/home', compact('homes'), [
'title'=>'Home',
'date' => $date,
'today' => $today
]);
}
Then its works 100%.....
Happy Coding and thanks :)

$project = new Project;
$cities = City::all();
$states = State::all();
$amenities = Amenity::query()->active()->get();
$brands = Brand::all();
$unit_types = UnitType::query()->active()->get();
$accommodations = AccommodationType::query()->active()->get();
return view('admin.projects._form', compact('project', 'cities', 'states', 'amenities', 'brands', 'unit_types', 'accommodations'));
You can send multiple variable like above code

Related

generating dynamic pdf with DOMPDF in laravel

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

undefined variable in view using dompdf

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

Creating default object from empty value laravel

I've been getting the error Creating default object from empty value laravelI was successful to insert new rows with the following code but today when I tried testing the code it is returning the error pointing on the line $reviw->rating = $request->productrating;.
Structure of my db table is:
id|fname|lname|email|country|title|content|rating|thumbnail|tour_id|status
public function store(Request $request)
{
// dd($request->all());
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'email' => 'required',
'country' => 'required',
'title' => 'required|min:10',
'productrating' => 'required',
'content' => 'required|min:10'
]);
// dd($request->productrating);
$review = new Review;
$review->fname = $request->fname;
$review->lname = $request->lname;
$review->email = $request->email;
$review->country = $request->country;
$review->title = $request->title;
$review->content = $request->content;
$reviw->rating = $request->productrating;
if($request->hasFile('fileupload1')){
$image = $request->file('fileupload1');
$filename = 'thumb'.time().'.'.$image->getClientOriginalExtension();
$location = 'images/client_review/'.$filename;
Image::make($image)->resize(200, 200)->save($location);
$review->thumbnail = $location;
}
$review->tour_id = $request->product_id;
$review->status = false;
$review->save();
Session::flash('success','Thank You for submitting us your review.');
return view('public.pages.message-review');
}
I'm sending following data from the form to save into the table.
I think you need to update your code like :
$review = new Review;
$review->fname = $request->fname;
$review->lname = $request->lname;
$review->email = $request->email;
$review->country = $request->country;
$review->title = $request->title;
$review->content = $request->content;
$review->rating = $request->productrating;
You have an error in $reviw->rating = $request->productrating; change $reviw to $review and it will work

Use of undefined constant while storing data from form

I have created a model
Review.php
and resource controller for it
ReviewController.php
with route
Route::resource('review','ReviewController');
and created a form to get the values. My store method:
public function store(Request $request)
{
$this->validate($request, [
'fullname' => 'required|max:255',
'email' => 'required',
'country' => 'required',
'tourname' => 'required',
'content' => 'required'
]);
$reviews = new Review;
$reviews->name = $request->fullname;
$reviews->email = $request->email;
$reviews->country = $request->country;
$reviews->content = $request->content;
$reviews->tour_id = $request->tourname;
if($request->hasFile('clidentpp')){
$image = $request->file('clidentpp');
$filename = time().'.'.$image->getClientOriginalName();
$location = public_path('images/client_review'.$filename);
Image::make($image)->resize(128, 128)->save($location);
$reviews->path = $location;
}
$reviews->save();
Session::flash('success','Thank you for your review !');
return back()->withInput(['tab'=>'complete']);
}
I'm getting error
Use of undefined constant reviews - assumed 'reviews'
pointing at line $reviews = new Review;. I tried changing$reviews to $review also still no luck.
It should be $reviews = new Review();
Try this.
$review[] = '';
$review['name'] = $request->fullname;
$review['email'] = $request->email;
...
Review::create($review);

How to use Request::user() inside a controller in Laravel 5?

I'm developing this to store users "Orders" in a batch.
My doubt is: How do I request user data inside the OrderController store()
I already debugged this and $id_user is null, probably because I'm already using Request::get for the form data.
I could pass the $id_user from the form inputs, but I guess it's not secure.
public function store(Request $request)
{
$quantities = Request::get('quantity');
$products = Request::get('id_product');
$hash_card = Request::get('hash_card');
$user = Request::user();
$id_user = $user->id_user;
$total_products = count($products);
for($i=0;$i<$total_products;$i++){
if($quantities[$i]>0){
$id_product = $products[$i];
$quantity = $quantities[$i];
$product=Product::find($id_product);
$product_price=$product->price;
$order_data[] = [
'id_user' => $id_user,
'id_product' => $id_product,
'quantity' => $quantity,
'product_price' => $product_price,
'hash_card' => $hash_card
];
}
}
Order::insert($order_data);
return redirect('pedidos?add&card='.$hash_card);
}
Any help is welcome!
Do you have debugging on, by any chance? Because this script shouldn't run at all. You shouldn't be able to call the method statically.
Try this:
public function store(Request $request)
{
$quantities = $request->quantity;
$products = $request->id_product;
$hash_card = $request->hash_card;
$id_user = $request->user()->id;
$total_products = count($products);
for($i=0;$i<$total_products;$i++){
if($quantities[$i]>0){
$id_product = $products[$i];
$quantity = $quantities[$i];
$product=Product::find($id_product);
$product_price=$product->price;
$order_data[] = [
'id_user' => $id_user,
'id_product' => $id_product,
'quantity' => $quantity,
'product_price' => $product_price,
'hash_card' => $hash_card
];
}
}
Order::insert($order_data);
return redirect('pedidos?add&card='.$hash_card);
}

Categories