You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since Laravel 10 (the following code worked fine with version 9), you will run into the following error when you try to create a model with a constructor.
ArgumentCountError: Too few arguments to function App\Models\Product::__construct(), 0 passed in /Users/me/Github/example-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php on line 67 and exactly 1 expected
Steps To Reproduce
Create a new Model
<?phpnamespaceApp\Models;
useIlluminate\Database\Eloquent\Factories\HasFactory;
useIlluminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
public$name;
publicfunction__construct($name)
{
parent::__construct();
$this->name = $name;
}
}
Instantiate it either in an endpoint or test file
ProductController.php
<?phpnamespaceTests\Unit;
useApp\Models\Product;
usePHPUnit\Framework\TestCase;
class ProductTest extends TestCase
{
/** @test */publicfunctionproduct_constructor_assigns_values_correctly()
{
$name = "Laptop";
$product = newProduct($name);
$this->assertEquals($name, $product->name, "The name should be initialized correctly.");
}
}
Laravel Version
11.9.2
PHP Version
8.3.6
Database Driver & Version
No response
Description
Since Laravel 10 (the following code worked fine with version 9), you will run into the following error when you try to create a model with a constructor.
ArgumentCountError: Too few arguments to function App\Models\Product::__construct(), 0 passed in /Users/me/Github/example-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php on line 67 and exactly 1 expected
Steps To Reproduce
ProductController.php
or
ProductTest.php
or (whatever way you prefer)
curl http://127.0.0.1:8000/product | grep "Too few arguments to function"
This was caused by #49843, reverting the content of HasEvents.php to the state before the change works fine.
Experienced this while trying to upgrade from 9 -> 10.
The text was updated successfully, but these errors were encountered: