Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thepsion5's avatar

I'd guess that @uxweb is correct, and the reason your seeder isn't inserting any data is because the model validation is failing.

1 like
jrdavidson's avatar

I guess I don't understand how it could work with being extended to eloquent but not base model. The code for the seed file isn't changing.

thepsion5's avatar

When it's extending from Eloquent, there's no model validation, so the save can't fail if some data doesn't match the validation rules. When you extend from BaseModel, you add the validation rules, which are capable of causing save() (and by extension, create()) to fail.

3 likes
jrdavidson's avatar

I wonder what I'm not understanding from that video lesson to be able to apply the same technique to my project for him to use it but not me.

thepsion5's avatar

Assuming that at this point you've removed the empty pointless constructor methods, the technique isn't the issue, your validation rules are. Here's what I think is happening:

  1. You call User::create() with Faker's data
  2. Your validate() method is called
  3. Some validation rule fails
  4. validate() returns false
  5. The function closure in saving() returns false
  6. The model is never saved to the database due to #5

Add this line to the your validation function, directly before return false;:

throw new \Exception('validation rules have failed when attempting to save model');

Then, run your seeder and see if you get that exception.

1 like
jrdavidson's avatar

I will when I get home. However explain why when I extend user model to eloquent then it runs successfully?

thepsion5's avatar

Because Eloquent doesn't do any validation before saving, whereas your BaseModel class does. If you're extending directly from Eloquent instead of your BaseModel class, steps 2-5 in my previous post don't ever happen.

1 like
jrdavidson's avatar

I apologize that I don't get this but tye rules themselves are right since it passes for eloquent. So the rules are right because I don't change them when I extend the user model to BaseModel.

uxweb's avatar

Why would you want to have validator within your models for seeding?. I mean, i think that is pointless because you are just generating random data to fill some records in the database.

Validation is used when you don't trust the data that is coming to your models from the outer world of the application.

If you are using this blindly you should try first doing validation as the documentation states, when a form is submitted.

I know this technique is cool, but if you don't really get it, well, then use other or try to understand it more deeply.

@thepsion5 and i have tried to help you to understand, but seems like you don't want to get it.

Maybe there are some gaps in knowledge that you should remove by watching some casts, like the Object Oriented Bootcamp or Laravel 4 From Scratch.

Never . Stop . Learning

1 like
jrdavidson's avatar

I'm trying to explain that all I'm wanting to do is run the seeder. I'm not trying validate anything.

jrdavidson's avatar

im trying to seed my database with data that's all I'm doing. I never said anything about me wanting to validate.

uxweb's avatar

Then, don't extend your models from BaseModel, just extend them from Eloquent and be happy :).

That technique you saw in a laracasts video is advanced and maybe is not for you right now.

thepsion5's avatar

Yep, keep in mind that Eloquent itself does not do any validation, that functionality is only added in your BaseModel class. When you called User::create(), it creates a new model instance, populates it with the data specified, and then tries to save it, which triggers the validation on your BaseModel class. That's why you could create users perfectly fine when the user class extended Eloquent, but not when it extended BaseModel.

Happy to help!

thepsion5's avatar

The only difference (as far as I can tell) is that your validation rules are different.

jrdavidson's avatar

Oh bummer. Thank you for checking. I am going to go home and add that exception line and see what happens.

jrdavidson's avatar

You were right I did receive that exception message. Is there a way I can find out what validation rules are not passing?

thepsion5's avatar

If you look at your BaseModel class, there's a getErrors function. That will return a messagebag object that you can use to view the errors.

jrdavidson's avatar

I can't do it from the seed file though correct? If not how would I retrieve them?

thepsion5's avatar

Well, instead of using User::create() in your seeder, you do something like this:

foreach(range(1, 20) as $index) {
    $user = new User([
            'first_name' => $faker->firstName, 
            'last_name' => $faker->lastName,
            'username' => $faker->username,
            'email_address' => $faker->freeEmail,
            'avatar' => $faker->lexify('????????'),
            'password' => Hash::make($faker->word),
            'role_id' => $faker->randomElement($roleIds),
            'status_id' => $faker->randomElement($statusIds)
    ]);
    $saved = $user->save();
    if(!$saved) {
        dd( $user->getErrors()->toArray() );
    }
}

The first time a user fails to be created, it will output the array of errors.

jrdavidson's avatar

Here's the response I got back.

array(1) {
  ["password"]=>
  array(1) {
    [0]=>
    string(41) "The password confirmation does not match."
  }
}

I kind of understand why. I have that confirmed validation error setup in the model however how can I get this to work without it applying that rule to the password since I'm only seeding the database.

jrdavidson's avatar

I understand how I have the rule in the model that it should be making sure that the password should be matching the confirm password but for the use of just seeding. I won't need to bother with a password confirm field so what is a way around this situation so it'll continue on?

Previous

Please or to participate in this conversation.