Saturday, February 17, 2018

Laravel model strange behaviour - development

Laravel model strange behaviour

Just curious why.

 public function get_number() {

    if(Model::get()->isEmpty()) {

        $number = Model::create(['number' => '1111111111']);

        $nubmer = $number->number; //(returns a... model, that's a suprise for me)

    } else {

        $number = Model::orderBy('number', 'desc')->first()->number;

        $number = Model::create(['number' => $number+1]);

        $number = $number->number; //(returns a property 1111111112 etc).

    }

return $number;

In both cases I am trying to get a property of a Model:

$number = $number->number;

But when “If” part works, it return a model (not property)…
when “else” part works, it returns a property (what i expected).

It means I don’t understand something in Laravel:)

Why does it return a full Model in “If” case?:)



from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-model-strange-behaviour/
via Lzo Media

No comments:

Post a Comment