Laravel在非对象

时间:2015-10-18 11:15:34

标签: php relationship factory laravel-5.1

我正在尝试使用某些模型工厂为数据库播种,但我收到错误call to member function create() on a non-object

以下是我的模型工厂:

$factory->define(App\Organisation::class, function ($faker) {
   return [
    'name' => $faker->company,
   ];
});

$factory->define(App\Department::class, function ($faker) {
   return [
    'name' => $faker->catchPhrase,
    'organisation_id' => factory(App\Organisation::class)->make()->id,
   ];
});

$factory->define(App\User::class, function ($faker) {
  return [
    'email' => $faker->email,
    'password' => str_random(10),
    'organisation_id' => factory(App\Organisation::class)->make()->id,
    'remember_token' => str_random(10),
  ];
});

在我的播种机中,我使用以下内容创建2个组织,并为每个组织创建用户和部门的关联,然后使用户成为该部门的经理:

factory(App\Organisation::class, 2)
        ->create()
        ->each(function ($o)
        {
            $user = $o->users()->save(factory(App\User::class)->make());

            $department = $o->departments()->save(factory(App\Department::class)->make());

            $department->managedDepartment()->create([
                'organisation_id' => $o->id,
                'manager_id' => $user->id,
            ]);


        });

但是我得到了fatalerrorexception call to member function create() on a non-object

我认为$department是一个对象?

我的部门模型如下:

class Department extends Model
{

    protected $fillable = ['name','organisation_id'];

    public function organisation()
    {
        return $this->belongsTo('App\Organisation');
    }

    /* a department is managed by a user */
    public function managedDepartment()
    {
       $this->hasOne('App\ManagedDepartment');
    }
}

我的managedDepartment模型如下:

class ManagedDepartment extends Model
{
    protected $table = 'managed_departments';


    protected $fillable = ['organisation_id', 'department_id', 'manager_id',];


    public function department()
    {
        $this->belongsTo('App\Department');
    }

    public function manager()
    {
        return $this->belongsTo('App\User');
    }
}

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

尝试回复你的关系

    public function department()
    {
        return $this->belongsTo('App\Department');
    }

在这里

 /* a department is managed by a user */
    public function managedDepartment()
    {
       return $this->hasOne('App\ManagedDepartment');
    }

我认为它可以解决您的问题。

答案 1 :(得分:0)

首先,不要让外键可填写! 其次,您的组织在ManagedDepartment中的功能在哪里?您应该创建一个,否则以下将无法工作,因为无法进行关联。 第三,我认为您应该在下面的

中将make()更改为create()
$factory->define(App\Organisation::class, function ($faker) {
   return [
    'name' => $faker->company,
   ];
});

$factory->define(App\Department::class, function ($faker) {
   return [
    'name' => $faker->catchPhrase,
    'organisation_id' => factory(App\Organisation::class)->create()->id,
   ];
});

$factory->define(App\User::class, function ($faker) {
  return [
    'email' => $faker->email,
    'password' => str_random(10),
    'organisation_id' => factory(App\Organisation::class)->create()->id,
    'remember_token' => str_random(10),
  ];
});

此外:

factory(App\Organisation::class, 2)
    ->create()
    ->each(function ($o)
    {
        $user = factory(App\User::class)->create();
        $o->users()->attach($user->id);

        $department = factory(App\Department::class)->create();
        $o->departments()->attach($department);

        $managedDep = new ManagedDepartment();
        $managedDep->associate($o);
        $managedDep->associate($user);
        $managedDep->associate($department);
        $managedDep->save();
    });