GatesPlan
GatesPlan

Reputation: 497

My code doesn't work outside laravel helper function dd()

Well.. I can't understand how this could happening but.. my code

$parser->mpml->images()->where('order', $parser->figureCounter)->first()->getUrl()

didn't work just outside dd() helper function. It produce

Call to a member function getUrl() on null error.

When inside,

dd($parser->mpml->images()->where('order', $parser->figureCounter)->first()->getUrl());

it works and produce proper url string...? This problem actually happens a week ago, it solved itself even I never do anything. Now, this happens again.

Anyone who have similar experience, would you share your knowledge?

Error must have reason and I can't find a tiny clue on this..


class FigureSubparser extends Subparser {
    public function start($attrs)
    {
        $this->attrs = $attrs;

        try {
            $order = $this->getEngine()->incFigureCounter()->getFigureCounter();
            $url = $this->getEngine()->mpml->images()->where('order', $order)->first()->getUrl();
        } catch (\Throwable $e) {
            dd($e->getMessage());
        }

        return '<img src="'.$url.'"/>';
    }
}

Above produce "Call to a member function getImageUrl() on null"


However, when I directly put $url part into dd(),

class FigureSubparser extends Subparser
{
    public function start($attrs)
    {
        $this->attrs = $attrs;

        // try {
            $order = $this->getEngine()->incFigureCounter()->getFigureCounter();
        //  $mpmlimage = $this->getEngine()->mpml->images()->where('order', $order)->first()->getUrl();
        // } catch (\Throwable $e) {
        //  dd($e->getMessage());
        // }

        dd($this->getEngine()->mpml->images()->where('order', $order)->first()->getUrl());

        return '<img src="'.$url.'"/>';
    }
}

In this time, dd() print out "http://localhost:8000/mpmlimages/NNYIPFxWz3TQGbtLJB2XEt9g8U8X370BarvRQ7oo.jpeg" which is what I intended.

and I'm sorry for a wrong answer that my laravel version. It was 5.7.19, not 5.4.


After solved.. Report.

The problem is indeed from loop.

While processing, because the xml_parser works event driven, every open tag makes another loop.

Each time the open tag is FIGURE element, my custom parser triggers FigureSubparser->start().

Problem is, if there exist parent object that has no image record, since I didn't check the existency of image record, code returns null.

Upvotes: 1

Views: 612

Answers (1)

George Hanson
George Hanson

Reputation: 3030

It sounds likely you are running this within a loop, so it is probably that for one of the results you are querying it is not returning anything. Because of this Laravel will return null which you are then trying to call the getUrl() method on.

Instead you should be checking that you have a result before calling the method, such as:

if ($record = $parser->mpml->images()->where('order', $parser->figureCounter)->first()) {
    $url = $record->getUrl();
}

Upvotes: 2

Related Questions