S
S
Sergey Bobrov2015-06-24 11:45:05
Laravel
Sergey Bobrov, 2015-06-24 11:45:05

Why is constructor called 3 times after firstOrCreate method in Laravel 5?

There is a constructor in the User class:

public function __construct() {
        parent::__construct();
        echo "__construct";
}

get User object
$users = \App\User::firstOrCreate(['email' => '[email protected]']);

We get the result:
__construct__construct__construct
Why is this happening?
And one more thing, if the object is not in the database, if there is an overridden constructor, errors occur, and if there is no constructor, everything is fine.
PDOException in Connection.php line 362:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (``.data`users`, CONSTRAINT `users_referrer_foreign` FOREIGN KEY (`referrer` ) REFERENCES `users` (`id`))
QueryException in Connection.php line 624:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`data`.`users`, CONSTRAINT `users_referrer_foreign` FOREIGN KEY (`referrer`) REFERENCES `users` (`id `)) (SQL: insert into `users` (`updated_at`, `created_at`) values ​​(2015-06-24 09:02:51, 2015-06-24 09:02:51))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Plisko, 2015-06-24
@tachini

Because everything is mixed up in the model: here are facades for calling like through statics, here is a query builder for queries, here are collections for working with one or a bunch of records, here is the model itself with attributes-metators-listeners. And to reset the states, re-creation of the object is often used in the style return new static;

O
orxan80, 2016-03-22
@orxan80

most likely the class is called there more than once, in the call of classes of heirs, this also happens

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question