I am trying to access a functions variable from another function in the same class. i am fairly new to the concept and I can get it to work in another function but when I try to create it's own function I get an Trying to get property of non-object I know what that means but it's confusing as to what needs to be returned in my function since it does work in my other function.
Function getting the error
public function getEditTotal($id) {
$techs = $this->technician();
$tech = $techs->tech;
var_dump($tech); die;
return View::make('report.edit', array('pageTitle' => 'Edit Report Total', 'id' => $id, 'tech' => $tech));
}`
The function I am trying to call
public function technician() {
$tech = DB::table('technician')
->get();
return $tech;
}
I had that same $tech variable in this function and it worked perfectly fine if I called $this->setComplete($id) instead.
Returned statement in the setComplete($id) function
return View::make('report.total', array('pageTitle' => 'Reporting', 'id' => $id, 'tech' => $tech, 'status' => $status));
I am sure it's just the way it's being returned since that variable is being returned in setComplete($id) in the array. I just don't know how to strictly call it in the technician() function.
When you call $techs = $this->technician(); you are setting the $techs to be whatever the value of the $tech variable in the technician function. That is going to be the result of DB::table('technician')
->get();
Theoretically this should be an array of objects where each object represents one row in the technician table.
If you want to know what's going on, add a var_dump($tech) inside the your technician() function, just prior to the return $tech statement.
Since you indicate it is working as expected, you're getting an array of objects. I'm not sure what you want to do with those, but inside the controller:
foreach ($techs as $tech) {
echo $tech->somefieldInTech;
}
or perhaps
echo $techs[0]->somefieldInTech;
So to be clear, in your laravel template, you might want to pass the entire $techs and foreach through it in the template, although from your code it's not clear what you need to do with the data.
Related
I am sorry for asking this basic level question. I have fetched some data from DataBase and stored it to a variable inside a function, and wanted to get the value of that variable outside the function?
public function getemailData()
{
$email_id_investors = $this->db
->select('value')
->get_where('common_email_settings', ['name' => investors_email])
->row()->value;
}
I wish to get the value of the $email_id_investors outside the function. Again I am apologizing for this basic question
Database table name - common_email_settings
fields are Id, title, name, value
1 Greeting Mail, greeting_email ,Greetings#investorsloanservicing.com
2 Loan Service Mail, loan_service_email ,LoanServicing#investorsloanservicing.com
3 Processing Mail, processing_email ,processing#investorsloanservicing.com
To strictly answer the question, you could store the value in a scoped global $this variable, though I don't know why you wouldn't just query the function and have it return a value.
public function getemailData($investors_email)
{
$this->email_id_investors = $this->db
->select('value')
->get_where('common_email_settings', ['name' => $investors_email])
->row()->value;
}
// then in another function called later in the chain, you can grab it
public function doSomethingElse() {
$investors = $this->email_id_investors;
}
It's probably better just to create a getter function for that variable.
This doesn't look useful given your scenario. This might be useful if the variable you're storing is something processor intensive (and you're using $this like a cache), you need to access it in multiple functions called during a given state, and you don't want to rewrite your function chain to accept this parameter (so as to pass it along). However, that is a clear sign you need to refactor your logic and make it more flexible (pass object or arrays rather than single variables for example).
You are not returning your variable.
Try returning your variable like this,
public function getemailData()
{
$email_id_investors = $this->db
->select('value')
->get_where('common_email_settings', ['name' => investors_email])
->row()->value;
return $email_id_investors;
}
function getemailData($name)
{
$email_id_investors = $this->db
->get_where('common_email_settings', ['name' => $name])
->result()[0];
return $email_id_investors->value;
}
This one worked for me. I have given this function in the common model page and called this on other pages.Thank you for your help
$email = $this->common->getemailData('account_email'); -> getting data in this variable
echo $email;
// exit();
I am trying to store an entry into a database but I get this error.
" Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, called in C:\xampp\htdocs\im-stuff\test\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869 "
request()->validate(['description' => 'required']);
$project->addTask(compact('description'));
Ok, so this does work, but next and better version doesn't.
$attributes = request()->validate(['description' => 'required']);
$project->addTask($attributes);
So this is the model which is being used.
public function tasks()
{
return $this->hasMany(Task::class);
}
public function addTask($description)
{
$this->tasks()->create(compact('description'));
}
At this point I am lost and really don't understand.
why are you using compact when you are saving the record to database instead as i mentioned in my comment it should be like this simple and easy:
public function addTask($description)
{
$this->tasks()->create($description);
}
here you can find the working of compact
Thanks
You should try to pass the attributes array directly to the create() function, e.g.:
public function addTask($description)
{
$this->tasks()->create($description);
}
Compact creates an array containing the variables passed, in your case:
print_r(compact('description'));
Array
(
[description] => your_array_of_attributes
)
So with compact() you are encapsulating your attributes array in another array giving them the key 'description'.
The compact() function is very useful when you want to pass data to the view, instead of writing this:
view('materials.index', ['materials'=>$materials, 'users'=>$users]);
You can write:
view('materials.index', compact('materials', 'users'));
I have array which contains laravel model (App\User) attribute names like following:
$documents = array(
'passport_expire' => 22,
'residency' => 13
);
If I called these functions directly it will return a boolean value like:
App\User::find(2)->passport_expire;//will output true / false
I want to execute the functions inside a foreach:
foreach($documents as $type => $val){
// I want to call the attributes
App\User::find(1)->{$type};
//I want to call and execute App\User::find(1)->passport_expire and App\User::find(1)->residency
I read in php documentation about a way with similar approach called Variable functions but I don't know how to accomplish that in laravel.
How about
App\User::find(1)->$type;
Also you want to use string & variable
App\User::find(1)->$type . '_test';
However, if you want to use string as first, I think you need to create a variable beforehand and use first approach.
$type = "xx_$type_xx"
App\User::find(1)->$type;
Whereas, App\User::find(1)->{$type}; works,
But App\User::find(1)->{$type . 'text'}; doesn't work.
You're calling the attributes/properties as functions?
So far I think you meant variable variables.
If you want to access the properties like passport_expire or residency then you can try like
$user = App\User::find(1);
foreach (array_keys($documents) as $document) {
$documents[$document] = $user->{$document};
}
The above code will update the $documents array with the values of $user object's properties.
But, if you want to execute as functions as you mentioned, you need to try call_user_func or call_user_func_array
I'm working on this method.
public function getTabs()
{
$tabs = [];
foreach ($this->campaigns as $index => $campaign) {
// return $campaign->getTitle();
// return get_class($campaign);
// $title = $campaign->getTitle();
// return $campaign->getTabTitle();
// return get_class($campaign);
// $title = $campaign->getTabTitle();
array_push($tabs, [
'title' => $title
]);
}
return $tabs;
}
The values in $this->campaign array, are instances of Campaign Object (for shure, due to typehinting in addCampaign method - not shown here but still).
If I would uncomment a line where I return the value rather than saving it to a variable, it works, but if I try to save it to the $title variable it throws an exception Call to a member function getTitle() on a non-object.
I tried it also with an other method but still no luck ...
Any ideas on how to solve this?
Why does PHP make a difference here? Would like to understand this process!
Thanks already to everyone taking the time to read this! :)
My best bet is that one of you entry in campaigns is not an object. Try to do a var_dump($this->campaings);.
It would work if you put the return because it would stop the foreach loop after the first element, but if you store it in a variable, it will try to call the getTitle() method on all the elements of your array.
Following code should help you to find out what are the problem with particular items:
foreach ($this->campaigns as $index => $campaign) {
var_dump(get_class($campaign));
}
If it won't help it would be great to take a look a the whole class. Maybe there is any other place where you are adding an item into an array. Maybe during initialization?
I am trying to print a variable fromm saveAction so i can see what is the value of that variable. I am learning Zend2 and i have continued where other developer stoped so i am trying to understand it better...This application is also using Doctrin.
I have this action which will save some data to database.
public function saveAction() {
$view = new ViewModel();
$logedUser = $this->getLogedUser();
print_r($_POST);
$shopId = (int) $this->params()->fromPost('shop_id', null);
print_r($shopId);
$shop = $this->getServiceLocator()
->get('Catalog\Model\Shop')
->getRepository()
->findOneBy(array('id' => $shopId, 'user' => $logedUser->getId()));
print_r($shop);
return $logedUser;
return $shop;
return $view;
}
I can print values from post, and variable shopId, but print_r($shop); doesn't show anything. How can i see the value of shop variable?
Try this
echo $shop->__toString();
Hi there you might want to try this one:
print_r($this->getAllParams());
instead of using $_POST
$shop = $this->getServiceLocator()
->get('Catalog\Model\Shop')
->findOneBy(array('id' => $shopId, 'user' => $logedUser->getId()));
please remove ->getRepository() from your code, then you will get your result.
At this point we dont know what the Service/Factory 'Catalog\Model\Shop' does. I assume it get's the Entity manager. The query looks ok to me 2. But having 3 return's is just wrong!
Your view will never have the $shop; nor the $view; variable in your .phtml view File. In ZF2 you can just return a Array to your view like so:
return array('shop' => $shop, 'logedUser' => $logedUser);
This will save those variables within your save.phtml
in /yourmodule/view/yourcontroller/save.phtml
If you want to set the .phtml matching yourself you indeed could use the view model like you tried in your code the procedure is a litle diffrent then. It could look like this:
$viewModel = new ViewModel(array(
'logedUser' => $logedUser,
'shop' => $shop,
));
$viewModel->setTemplate('yourmodul/yourcontroller/save');
return $this->getContainerViewModel($viewModel);
Once you have that you can just access your variables in the save.phtml template file like so:
<?php echo $shop->getSomething(); ?>
You did not show us the Shop entity but I assume it has methods like getName(), __toString() etc. You can just call these Object methods within your view.
If you still have trouble understanding the mvc concept of zf2 you might want to re-read the zf2 documentation.
You have to show us what Catalog\Model\Shop and what query are you using for getting your data. Is there possibility that your query just getting null because it fails to get anything from database ? Do you have any kind of checks in your model to find out is your result from query good?