Larastan: Call to an undefined method Illuminate\Support\Fluent::onDelete()

Created on 2 Nov 2020  路  9Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.6.9
  • --level 5:
  • Laravel: 8.12.3

    Description

Laravel code where the issue was found

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->foreignId('author_id')->constrained()->onDelete('cascade'); // <-- HERE
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->dropForeign(['author_id']);
            $table->drop();
        });
    }
}

Result of runing ./vendor/bin/phpstan analyse:

 ------ --------------------------------------------------------------------
  Line   database\migrations\2020_11_02_133706_create_books_table.php
 ------ --------------------------------------------------------------------
  19     Call to an undefined method Illuminate\Support\Fluent::onDelete().
 ------ --------------------------------------------------------------------

How can I override that the return type of method foreignId()->constrained() is not Fluent, but ForeignIdColumnDefinition instead?

enhancement help wanted

Most helpful comment

How can I override that the return type of method foreignId()->constrained() is not Fluent, but ForeignIdColumnDefinition instead?

/** @var ForeignIdColumnDefinition $definition */
$definition = $table->foreignId('author_id')->constrained();
$definition->onDelete('cascade'); 

All 9 comments

Yes, this is a known thing.

How can I override that the return type of method foreignId()->constrained() is not Fluent, but ForeignIdColumnDefinition instead?

/** @var ForeignIdColumnDefinition $definition */
$definition = $table->foreignId('author_id')->constrained();
$definition->onDelete('cascade'); 

Thanks!

You're welcome.

@canvural Do we plan to support the constrained() method?

I think it's more about adding support for Illuminate\Support\Fluent

I don't have any plans, but if someone wants to implement it I will accept a PR! :+1:

I started seeing the following errors after updating my dependencies today.

Call to an undefined method Illuminate\Database\Eloquent\Relations\HasMany::first().
Call to an undefined method Illuminate\Database\Eloquent\Relations\HasMany::count().  
Call to an undefined method Illuminate\Database\Eloquent\Relations\HasMany::where().
etc.

Is this the same problem as OP?

@PHLAK Your issue unrelated to the OP. Yours is related to issues like #824 #804 I think you can find the solution there, if not open a new issue.

Thanks @canvural, that was what I needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zlodes picture zlodes  路  3Comments

Gummibeer picture Gummibeer  路  3Comments

tranba picture tranba  路  4Comments

szepeviktor picture szepeviktor  路  4Comments

iNviNho picture iNviNho  路  3Comments