I'd guess that @uxweb is correct, and the reason your seeder isn't inserting any data is because the model validation is failing.
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.
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.
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.
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:
- You call
User::create()with Faker's data - Your
validate()method is called - Some validation rule fails
validate()returns false- The function closure in
saving()returns false - 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.
I will when I get home. However explain why when I extend user model to eloquent then it runs successfully?
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.
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.
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
I'm trying to explain that all I'm wanting to do is run the seeder. I'm not trying validate anything.
im trying to seed my database with data that's all I'm doing. I never said anything about me wanting to validate.
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.
I do want to thank @thepsion5 and @uxweb for helping me.
My pleasure @xtremer360 .
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 Is his BaseModel class doing something that mine is not in that video lesson?
The only difference (as far as I can tell) is that your validation rules are different.
Oh bummer. Thank you for checking. I am going to go home and add that exception line and see what happens.
You were right I did receive that exception message. Is there a way I can find out what validation rules are not passing?
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.
I can't do it from the seed file though correct? If not how would I retrieve them?
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.
Wow that's good code. Let me try that out.
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.
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?
Please or to participate in this conversation.