无法与多个子模型建立关系

时间:2019-04-12 06:11:13

标签: laravel eloquent model

我有三个模型成员,贷款和储蓄。贷款和储蓄成为会员的孩子。但是,我只能与贷款建立关系。当我与储蓄建立关系时,它将功能视为非静态。

Member.php

use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
 protected $fillable=['name','address','phone']; 
 public function loan()
{
    return $this->hasMany(Loan::class,'member_id','id');
}
 public  function saving()
{
    return $this->hasMany(Saving::class,'member_id','id');
}

}

Saving.php

  use Illuminate\Database\Eloquent\Model;
  class Saving extends Model
  {
  protected $fillable=['amount','member_id']; 
   public function member()
   {
    return $this->belongsTo('App\Member','id','member_id');
    }

}

函数save()给出以下错误。

 Error::Cannot make static method Illuminate\Database\Eloquent\Model::saving() non static in class App\Member

我以前没有遇到过这个问题,也没有核心概念。

2 个答案:

答案 0 :(得分:1)

您在belongsTo关系中提供了错误的密钥,请提供以下内容:

public function member()
{
return $this->belongsTo('App\Member','member_id','id');
}

答案 1 :(得分:1)

在您的Model.php中更改

public function loan()
{
    return $this->hasMany('App\Loan','member_id','id');
}
 public  function saving()
{
    return $this->hasMany('App\Saving','member_id','id');