Larastan: Access to an undefined property Illuminate\Database\Eloquent\Model::attribute

Created on 19 Oct 2020  ·  4Comments  ·  Source: nunomaduro/larastan

  • Larastan Version: 0.6.6
  • --level used: 5
  • Laravel Framework 6.18.43

Description

I'm getting Access to an undefined property Illuminate\Database\Eloquent\Model::$status, it happens only if the model is injected in the __construct method.

Laravel code where the issue was found

OrderController.php

    use App\Order;

    /**
     * @var Order
     */
    protected $orderModel;

    /**
     * @param Order $orderModel
     */
    public function __construct(Order $orderModel){
        $this->orderModel = $orderModel;
    }

In another method I have:

$order = $this->orderModel
            ->with('items')
            ->with('parcels')
            ->find($invoiceId);
$order->status = Order::INCOMPLETE;

It won't happen if I do:

$order = Order::with('items')
            ->with('parcels')
            ->find($invoiceId);
$order->status = Order::INCOMPLETE;
bug

All 4 comments

Hi,

Thank you for the report. I can confirm this is a bug. $this->orderModel->with() returns Builder without generic type for the model specified. So the default Model is used.

I can fix this, but I'd also argue that injecting a model instance and then calling find on it does not make much sense. The second example makes more sense.

🐎 6 minutes.

🐎 6 minutes.

After 11PM ❗

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bogdankharchenko picture bogdankharchenko  ·  4Comments

fgilio picture fgilio  ·  4Comments

zlodes picture zlodes  ·  3Comments

JeroenVanOort picture JeroenVanOort  ·  3Comments

iNviNho picture iNviNho  ·  3Comments