关系方法必须返回一个类型的对象照明 database eloquent ressisth resisati



我正在尝试使用laravel雄辩的关系创建,但是我会遇到此错误

Relationship method must return an object of type IlluminateDatabaseEloquentRelationsRelation

这是我要在控制器中要做的

$data = $request->all();
    $company = Company::create([
      'name' => $data['name'],
      'description' => $data['description'],
    ]);
    $company->members->create([
        'name' => $data['name'],
        'email' => $data['email'],
        'status' => $data['status'],
        'password' => bcrypt($data['password']),
    ]); 

这是我的公司模型

class Company extends Model
{
protected $fillable = [  'name', 'description'];
public function members(){
  $this->hasMany('AppUser');
}
public function reports(){
  $this->hasMany('AppReport');
}
}   

这是我的用户模型

class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
    'name', 'email', 'password', 'company_id','status',
];
protected $hidden = [
    'password', 'remember_token',
];

public function company(){
  $this->belongsTo('AppCompany');
}

这是我遇到的错误

(1/1) LogicException
Relationship method must return an object of type 
IlluminateDatabaseEloquentRelationsRelation
in HasAttributes.php (line 403)
at Model->getRelationshipFromMethod('members')
in HasAttributes.php (line 386)
at Model->getRelationValue('members')
in HasAttributes.php (line 316)
at Model->getAttribute('members')
in Model.php (line 1262)
at Model->__get('members')
in AdminController.php (line 48)
at AdminController->addCompany(object(Request))
at call_user_func_array(array(object(AdminController), 'addCompany'), array(object(Request)))
in Controller.php (line 55)
at Controller->callAction('addCompany', array(object(Request)))
in ControllerDispatcher.php (line 44 )

如何解决此错误?

您已经忘记了这样的返回关系:

public function company(){
    return $this->belongsTo('AppCompany');
}
public function members(){
    return $this->hasMany('AppUser');
}
public function reports(){
    return $this->hasMany('AppReport');
}

相关内容

  • 没有找到相关文章

最新更新