Jenssen
Jenssen

Reputation: 1881

Php helper function parameter undefined

In my Laravel 5.4 project I've got a Helpers.php file. That's working great.

Now I've made a helper that looks like this:

if (! function_exists('issetWithReturn')) {
    /**
     * @return mixed
     */
    function issetWithReturn($values)
    {
        return isset($collection) ? $collection : '';
    }
}

In my OrganisationController.php I use it like this:

/**
 * Show all organisations.
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function index()
{
    if (Gate::allows('edit-organisations')) {
        $products = $this->productRepo->getAll();
    }

    return view('organisation.index')->with([
        'products'   => issetWithReturn($products),
    ]);
}

But my editor already gives a sign that $products within issetWithReturn is undefined? Why is that?

When I try this it's all working:

'products'   => isset($products) ? $products : '',

Upvotes: 0

Views: 114

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

Well you are passing a parameter called $values but inside the function using a varible called $collection

So basically its a typo

if (! function_exists('issetWithReturn')) {
    /**
     * @return mixed
     */
    function issetWithReturn($values)
    {
        return isset($value) ? $value : '';
    }
}

Upvotes: 1

Related Questions