Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentCountError: Constructor in App\Models\Product Requires Arguments When None Provided by Eloquent Event #51689

Closed
Disservin opened this issue Jun 3, 2024 · 1 comment

Comments

@Disservin
Copy link

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

  1. Create a new Model
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    public $name;

    public function __construct($name)
    {
        parent::__construct();

        $this->name = $name;
    }
}
  1. Instantiate it either in an endpoint or test file

ProductController.php

<?php

namespace Tests\Unit;

use App\Models\Product;
use PHPUnit\Framework\TestCase;

class ProductTest extends TestCase
{
    /** @test */
    public function product_constructor_assigns_values_correctly()
    {
        $name = "Laptop";

        $product = new Product($name);

        $this->assertEquals($name, $product->name, "The name should be initialized correctly.");
    }
}

or

ProductTest.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use Illuminate\Http\Response;

class ProductController extends Controller
{
    public function store(Request $request)
    {

        $product = new Product("foo");
        
        $product->save();

        return response()->json($product, Response::HTTP_CREATED);
    }
}
  1. Execute the code
./vendor/bin/phpunit --filter ProductTest

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.

@Disservin
Copy link
Author

Disservin commented Jun 3, 2024

Sorry I just saw that this is a duplicate of #50284.

@Disservin Disservin closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant