Larastan: model, query builder functions not found

Created on 22 Oct 2018  路  11Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.3.4
  • --level used: 2

Description:

I get errors for the query builder methods on models. (first, where, get, whereDoesntHave, delete, pluck ...)

Laravel code where the issue was found:

Error: Call to an undefined method App\Http\Controllers\SomeController::first().

ModelName::where('publication_id', '=', $id)
                ->where('position', '=', $position + 1)
                ->first();

Errors:
Call to an undefined method App\Http\Controllers\LocationController::with().
Call to an undefined method App\Http\Controllers\LocationController::where().
Call to an undefined method App\Http\Controllers\LocationController::orderBy().

$location = Location::where('site_id', $siteId)->orderBy('title')->with('page')->get();

If anyone can help me that should be very nice.

If it is something in larastan and someone can point me in the right direction, I like to try to create a PR.

Support fixed

All 11 comments

This is an issue in your code. Can you share the exact code where this line exists Location::where('site_id', $siteId)->orderBy('title')->with('page')->get();?

I see that only the with error was from that line, the other errors where from more below in the code.

The code (I did remove the rest of the functions that isn't part of this)

namespace App\Http\Controllers\Admin;

use App\Location;
use App\Page;

class LocationController extends Controller

    public function index()
    {
        $siteId = $this->findAndSetSessionSiteId('location');

        return view('admin.locations.index', [
            'locations' => Location::where('site_id', $siteId)->orderBy('title')->with('page')->get(),
        ]);
    }

    public function create()
    {
        $pages = Page::where('site_id', $siteId)
            ->where('parent_id', 0) // this is the line wiht the where error
            ->where('active', 1)
            ->orderBy('pos')
            ->get();
        // rest of the function
    }

    public function edit(Location $location)
    {
        $pages = Page::where('site_id', $location->site_id)
            ->where('parent_id', 0)
            ->orderBy('pos') // error of the orderBy function
            ->get();
        // rest of the function
    }

Location and Page both extend Illuminate\Database\Eloquent\Model.

Not sure that's an issue in his code, I have the exact same issue in various (but not all) calls on Eloquent instances:

Call to an undefined method App\Http\Controllers\<snip>::orWhere().
Call to an undefined method Illuminate\Database\Eloquent\Relations\Relation::save().
Call to an undefined method Illuminate\Database\Eloquent\Relations\Relation::sync().
Call to an undefined method App\Http\Controllers\<snip>::get().
Call to an undefined method Illuminate\Database\Eloquent\Relations\HasMany::withTimestamps().

I am noticing the same, and, it seems to related to chaining multiple where:
"nunomaduro/larastan": "^0.3.4"

namespace App;
use App\Models\Customer;

class Stan
{
    public function useWhere()
    {
        return Customer::where('email', '[email protected]')->where('name', 'test')->first();
    }

    public function useWhereSingleWhere()
    {
        return Customer::where([
                        'email' => '[email protected]',
                        'name' => 'test'
                      ])->first();
    }

}
php artisan code:analyse --paths=app/Stan.php 
 1/1 [鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔鈻撯枔] 100%

 ------ ------------------------------------------------ 
  Line   app/Stan.php                                    
 ------ ------------------------------------------------ 
  10     Call to an undefined method App\Stan::first().  
 ------ ------------------------------------------------ 

I think it might be issue with methods returning $this. I get same error message for ->count() missing on controller.

Yes, it looks like a $this-related issue.
I have this code:

declare(strict_types=1);

namespace App\Html\Form;

use App\Models\Country;

class CountrySelect extends Select
{
    public function __construct(string $id, string $title, bool $editable = true)
    {
        $options = Country::orderBy('order')->orderBy('name')->pluck('name', 'id');

        parent::__construct($id, $title, $editable, $options);
    }
}

And I get this error:

Call to an undefined method App\Html\Form\CountrySelect::pluck().

Method pluck is called on Country, but larastan thinks that it is called on CountrySelect.

@nunomaduro Do you agree with this analysis? Or do you think it's something else?

I think all these should have dynamic return types as they return the same type what they are given.

@tvbeek @ThomHurks This issue has been fixed in Larastan v0.3.10.

Please run a composer update nunomaduro/larastan and tell me the results.

@nunomaduro Thanks! The issue where it thinks that the Eloquent methods should be present on the current file is gone :)

It is indeed fixed, thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielcosta picture danielcosta  路  4Comments

gitetsu picture gitetsu  路  4Comments

tranba picture tranba  路  4Comments

hailwood picture hailwood  路  4Comments

JeroenVanOort picture JeroenVanOort  路  3Comments