1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, that's exactly what I get. Most of the time I would just set the table up and foreach it in the template. What should the return be then if i am just wanting to return that entire piece? So, if I want to loop through it on other pages or functions I can. I am basically wanting exactly what the var_dump is on the return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.