0
當試圖運行php artisan db:seed
我得到的錯誤:雄辯不能從faker的關係播種?
PHP Fatal error: Call to a member function save() on null in /var/www/html/project/database/seeds/UserTableSeeder.php on line 31
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function save() on null
和我的文件
應用程序/模型/ user.php的
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $dates = [
'deleted_at'
];
public function profile()
{
$this->hasOne('\App\Models\Profile');
}
}
應用程序/模型/ Profile.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
//
protected $table = 'profile';
protected $dates = [
// 'published_at',
'deleted_at'
];
public function user()
{
$this->belongsTo('\App\Models\User');
}
}
create_user_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamp('deleted_at');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('user');
}
}
create_profile_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profile', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name');
$table->text('bio');
$table->string('avatar');
$table->date('dob');
// $table->timestamp('published_at');
$table->timestamp('deleted_at')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('user')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('profile');
}
}
UserFactory.php
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = $faker->words('2',true),
'remember_token' => str_random(10),
];
});
ProfileFactory.php
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
$factory->define(App\Models\Profile::class, function (Faker\Generator $faker) {
return [
'name' => $faker->firstname().' '.$faker->firstname(),
'bio' => $faker->words('50',true),
'avatar' => $faker-> imageUrl(40,40),
'dob' => date('Y-m-d'),
];
});
UserTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
factory(App\Models\User::class, 5)
->create()
->each(function($user){
dd($user);
$user->profile()->save(factory(App\Models\Profile::class)->make()) ;
}
}