DOMPDF loadView() error - undefined variable: data - php

I'm currently trying to incorporate the DOMPDF Wrapper for Laravel into my project, however I'm having troble figuring out how to pass a variable into the PDF template.
As per the instructions, in my controller I have:
//PrintController.php
$data = array('name'=>'John Smith', 'date'=>'1/29/15');
$pdf = PDF::loadView('contract', $data);
return $pdf->stream('temp.pdf');
and in my view:
//contract.php
...
<p><?php echo $data->name ?><p>
<p>Signature</p>
But when I try to render the page, I get the error:
ErrorException (E_UNKNOWN)
Undefined variable: data
I'm not sure why the loadView() method is not passing the $data variable to the view. Is there a step I'm missing in setting it up in the controller and/or view?

The loadView method you are using is going to use the extract method before passing the data to the views. This method extracts all the array elements, and creates variables for them based on the key of the element
This means that your array keys are going to be your variable names, ie $name and not $data->name. This is fairly standard in laravel, for example when using Blade Views.
http://php.net/manual/en/function.extract.php

Use compact('data') instead of $data.
Example:
$pdf = \PDF::loadView('productType.invoice', compact('data'));

Related

defining variables in a blade laravel

I'm very new here please.
I'm trying to get some information and pass it to the laravel blade to be displayed on my website. What is for sure is that the call was successful but when I try passing it to the view I get an error that says "undefined variable" in the view page and sometimes the area where information was supposed to be displayed just shows an empty space.
Pass your variables in the view method instead. The with function is for passing individual data, so it doesn't accept an array. Example:
return view('profile.newaddress', [
'address'=>$address
]);
If you want to use with, try it like this instead (assuming $address in a string):
return view('profile.newaddress')->with('address', $address);

undefined variable error in blade view

I am currently working with Laravel 5.2, trying to display images on click
which I have currently stored in the Storage folder. I am trying to display these images in my blade view but every time it loads the page, it gets to an undefined variable exception.
Controller:
public function createemoji($action,$statusId)
{
$path = storage_path('app/public/images/'.$action.'.gif');
/*$request=new storage();
$request->comment=$path;
$request->user_id=Auth::user()->id;
$request->post_id=$statusId;
$request->save();*/
return redirect()->returnemoji()->with('file'->$path);
}
public function returnemoji($file)
{
return Image::get('$file')->response();
}
In my default view I tried using count() but everytime it loads the page, it gives me Undefined variable. How should I display it?
Try to change this:
->with('file'->$path);
To this:
->with('file', $path);
https://laravel.com/docs/5.3/views#passing-data-to-views
With function takes two arguments key and value
You can use this
return redirect()->returnemoji()->with('file',$path);
You can try this out:
Instead of:
return redirect()->returnemoji()->with('file'->$path);
Try this:
return $this->returnemoji($path);
Hope this helps you.
There are a few problems.
Single quotes do not process variables, so instead of this
return Image::get('$file')->response();
You could do this
return Image::get("$file")->response();
or
return Image::get("{$file}")->response();
but none of this is necssary since you are just using the variable by itself without any additional formatting, so remove the quotes altogether
return Image::get($file)->response();
The object operator -> is used in object scope to access methods and properties of an object. Your function returnemoji() is not a method of RedirectResponse class which is what the redirect() helper method returns.
The with() method is not appropriate here, you just need to pass a parameter to a function like this
return redirect()->returnemoji($path);
Optionally, I recommend following the PSR2 code style standard which includes camel cased variable names so createemoji() should be createEmoji(). Also I think you can usually omit response() when returning most data types in Laravel as it will handle that automatically for you.
I think you have to try the following:
Instead of:
return redirect()->returnemoji()->with('file'->$path);
Try this:
return redirect()->returnemoji($path);
And yes, remove the quotes from this:
return Image::get('$file')->response();

undefined $load property when loading view in laravel 4

I need to do the process of loading a view inside my controller which can be done using something like:
$main['menu'] = $this->load->view('myView', NULL, TRUE);
but when executing I receive an error saying
Undefined property: MainController::$load
How can this be fixed, or if you can give me another way to do the work
You can generate view in your controller with:
// get the view object
$view = View::make('myView'));
// get view content as string
$content = $view->render();
// pass the content to another view
$anotherView = View::make('anotherView', array('content' => $content));
You can read more about how to use views in Laravel 4 here: http://laravel.com/docs/4.2/responses#views. I suggest you have a look here as it seems that what you're trying to do is not the standard way of doing stuff in Laravel.

Passing 2 arrays from controller to view

I'm trying to pass two arrays from controller to view, using this approach:
My underlying data query has extracted this data as follows:
$catalogData: Title (Clothing); Season (Winter);
$ProductData: Type (Shirts); Size (XL); Price ($10);
Controller
$this->load->view('users/TheView', $catalogData, $productData);
View
<?php
echo $Catalog;
echo $Season;
echo $Type;
echo $Size;
echo $Price;
?>
My error message is
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Catalog
Filename: users/controller.php
I cant seem to find any examples of passing two arrays to a view, which makes me think it's not possible?
Edit: I'm using CodeIgniter
Without knowing anything about the MVC framework in question, I can only assume it expects one array argument after the view name and uses extract, in which case I'd do the following
$this->load->view('users/TheView', array(
'catalog' => $catalogData,
'product' => $productData));
and in your view...
<?php
echo $catalog['Title'], $catalog['Season'], $product['Type'], etc
Also, your error message seems to indicate that you should be using $Title instead of $Catalog. Title is the property name shown in your data example.
First thing is if you want to pass more than one array to view then you should do something like this in controller
$data=array();
$data['catalogData']=$this->model_name->function_name(); // query for catalog
$data['ProductData']=$this->model_name->function_name(); // query for product
$this->load->view('view_name',$data);
in view
if(isset($catalogData) && is_array($catalogData) && count($catalogData)>0)
{
echo $catalogData['Title'];
}
these is the procedure.Please let me know if you face any problem.
You can pass two arrays like this
$data['catalogData'] = $catalogData;
$data['productData'] = $productData;
$this->load->view('users/TheView', $data);
A couple different thing I can think of,
First: you are trying to echo $Catalog, but I don't see anywhere that you are passing a variable with that name to your view. This leads to the second part.
Second: You need to echo out the object you are trying to print, not the Array. so they should be something like:
<?php echo $catalogData['Title']; ?>
Finally, I honestly never do php programing without a framework, and you don;t mention if you are using one, but typically in those you pass an array of variables into your view. So something like:
$this->load->view('users/TheView', array('catalogData'=>$catalogData, 'productData'=>$productData);

Zend Passing variable to PartialLoop inside partial view

I have in my view a partial containing a partialLoop.
But when I run the page I have the following error message:
Call to a member function countComments() on a non-object in ...'_loop.phtml'
This is how I call my partial from within my view:
echo $this->partial('_post.phtml',$this->post);
where $this->post is a DB retrieved row
This is my partial's content:
MY simplified Partial!
echo $post->countComments();//the count number is correctly output..
echo $this->partialLoop('_loop.phtml',$this->object);
This is my partialLoop's content:
echo $this->object->countComments();//no output!
In the bootstrap I have set:
$view->partial()->setObjectKey('object');
$view->partialLoop()->setObjectKey('object');
Is this the right way to call partialLoops from within partials??
P.s. I var_dumped $this->object inside my partial and it is a PostRow OBJECT.I var dumped $this->object into _loop.phtml and I have 5 NULLS (standing for id,title,text,author,datetime fields of my post)
thanks
Luca
I think that the reason is that when you pass $this->post into partial view helper like this:
$this->partial('_post.phtml',$this->post);
partial view helper will execute its toArray() method. Hence, your $this->object is an array and you are passing an array to your partialLoop. So, in your partialLoop you are trying to execute countComments() on an array representing your row post object, rather than actual row object.
To avoid this, I would recommend passing variables to partial and partialLoop view helpers using array notation, e.g:
$this->partial('_post.phtml',array('post' => $this->post));
Hope this helps.
This error is caused by the default behaviour of the partial and partialLoop view helpers as Marcin said above.
Although it is confusing the manual does explain this here
Object implementing toArray() method. If an object is passed an has a
toArray() method, the results of toArray() will be assigned to the
view object as view variables.
The solution is to explicitly tell the partial to pass the object. As the manual explains:
// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');
// Tell partial to pass objects from partialLoop as 'model' variable
// in final partial view script:
$view->partialLoop()->setObjectKey('model');
This technique is particularly useful when passing
Zend_Db_Table_Rowsets to partialLoop(), as you then have full access
to your row objects within the view scripts, allowing you to call
methods on them (such as retrieving values from parent or dependent
rows).

Categories