-
As I mentioned in title, I have a problem with the student model factory. <?php
namespace App\Models\Student;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Student extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'full_name',
'national_code',
'birthdate',
'gender',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
public function father()
{
return $this->hasOne(StudentFather::class);
}
public function mother()
{
return $this->hasOne(StudentMother::class);
}
public function contactInfo()
{
return $this->hasOne(StudentContactInfo::class);
}
} The Student model factory ( <?php
namespace Database\Factories;
use App\Models\Student;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\Factories\Factory;
class StudentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Student::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$nationalCode = $this->faker->randomNumber(10, true);
return [
'full_name' => $this->faker->name,
'national_code' => $nationalCode,
'birthdate' => now(),
'gender' => Hash::make($nationalCode),
'password' => $this->faker->randomElement(['male', 'female'])
];
}
} When I try to use StudentFactory through App\Models\Student\Student::factory()->count(3)->make(); Result:
Laravel Version: 8.28.1 Is there anything that I did wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
AmRo045
Mar 8, 2021
Replies: 1 comment
-
The problem solved by adding /**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\StudentFactory::new();
} and fixing the Student model namespace in factory class: use App\Models\Student; => use App\Models\Student\Student; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AmRo045
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem solved by adding
newFactory
method to Student model:and fixing the Student model namespace in factory class: