Reputation: 202
im trying to create a new project with everything to know more about laravel, for now im creating models, migrations and seeds with factory and im running into this problem:
Model User
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Model implements Authenticatable
{
protected $table = 'user'; //name of the table in database
protected $primaryKey = 'Id'; //Primary Key of the table
/**
* Relations between tables
*/
public function GetLoginInfo()
{
return $this->hasMany('App\Models\LoginInfo', 'UserId');
}
public function getStatus()
{
return $this->belongsTo('App\Models\AccountStatus');
}
}
Model Account status
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AccountStatus extends Model
{
protected $table = 'account_status'; //name of the table in database
protected $primaryKey = 'Id'; //primary Key of the table
public $timestamps = false; //true if this table have timestaps
/**
* Relations between tables
*/
public function GetUsers()
{
return $this->hasMany('App\Models\Users', 'StatusId');
}
}
Seed file:
<?php
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\User::class, 5)->create();
}
}
factory file:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
//Factory for Account Status table
$factory->define(App\Models\AccountStatus::class, function (Faker\Generator $faker) {
return [
'Description' => $faker->word,
];
});
//Factory for user table
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
return [
'Username' => $faker->unique()->userName,
'Password' => bcrypt('test'),
'Email' => $faker->unique()->safeEmail,
'Name' => $faker->name,
'StatusId' => Factory(App\Models\AccountStatus::class)->create()->id,
];
});
when trying to db seed with artisan:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Models\Model' not found
already trying composer dump-autoload , optimize and i have the models in a folder in App\Models.
the seeds with factory for the account status work, but when i try to run for both (account status then user) i have that error) anyone knows why? and is good practice to have all the factory code in 1 file?
Upvotes: 1
Views: 5710
Reputation: 17668
In your User
Model you are extending Model
class whereas you should extend Authenticatable
class alias.
So your User
model will look as:
class User extends Authenticatable
Upvotes: 1