0

I'm running the following code in Laravel to load various relationships on my Product model:

$product->load([
        'skus' => function($query){

                $query->select($this->skuFields)->with([

                        'uniqueItem' => function($query){
                                // <----------reuse the code below--------->
                                $query->with([
                                        'fulfillmentCenterUniqueItems',
                                        'products' => function($query){

                                                $query->select($this->productFields)->with([
                                                        'skus' => function($query){
                                                                $query->select($this->skuFields);
                                                        }
                                                ]);
                                        },
                                        'skus' => function($query){
                                                $query->select($this->skuFields);
                                        }
                                ]);
                               // <----------reuse the code above--------->
                        }
                ]);
        },
        'uniqueItem' => function($query) {

                //need to reuse code here
        },
]);

As you can see from my note in the code, there is a place where I would like to reuse some code, so I was hoping to place it in a function, and reuse it.

Therefore, I did the following:

$uniqueItemLoadFunction = function($query)
{

        $query->with([
                'fulfillmentCenterUniqueItems',
                'products' => function($query){

                        $query->select($this->productFields)->with([
                                'skus' => function($query){
                                        $query->select($this->skuFields);
                                }
                        ]);
                },
                'skus' => function($query){
                        $query->select($this->skuFields);
                }
        ]);
};

$product->load([
            'skus' => function($query) use ($uniqueItemLoadFunction){

                    $query->select($this->skuFields)->with([

                            'uniqueItem' => $uniqueItemLoadFunction($query)
                    ]);
            },
            'uniqueItem' => function($query) {

                    //need to reuse code here
            },
    ]);

However, I now receive a BadMethodCallException:

Call to undefined method Illuminate\\Database\\Query\\Builder::fulfillmentCenterUniqueItems()

This error was not occurring when the code was run the original way. This makes me think I am not using an anonymous function correctly. How can I make this work?

1
  • in your case, there is other error, message is saing, that in product model does not exist 'fulfillmentCenterUniqueItems' function Commented Jan 22, 2015 at 22:31

1 Answer 1

1

You are on the right way. However: The uniqueItem-key expects a function. But you are actually calling the function right away and only give the retuned value back to the key (which is null in this case). When now laravel tries to execute the given function it tries to execute null() which is not possible.

Long story short: Remove the brackets

'uniqueItem' => $uniqueItemLoadFunction

This way you assign the reference of the function to the key, not the returned value.

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

Comments

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.