ErrorException 非静态方法不应静态调用 - 当尝试从两个不同的 Table 中获取数据时

ErrorException Non-static method should not be called statically - When Try to Fetch Data from Two Different Table

我想从不同的 table 获取数据。

我有 2 个 table:

  1. teacher_profiles
  2. user_types

用户类型模型:

public function teacher()
{
    return $this->hasMany('App\TeacherProfile'); 
}

TeacherProfile 模型:

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

控制器:

$usertype = TeacherProfile::usertype()->with('teacher');
return view('teachers.create')->with($usertype);

查看:

@if ($usertype)
  @foreach($usertype as $data)
    <input type="text" name="name" id="input-name" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" placeholder=""  value="$data->id" required autofocus>
 @endforeach
@endif

然后我遇到了 ErrorException 非静态方法 App\TeacherProfile::usertype() 不应静态调用错误

我想要做的是在用户类型中显示值为“2”的 'teacher',如下图所示:

请在表达式末尾添加get()、first()。这可能会奏效。

$usertype = TeacherProfile::with('usertype.teacher')->get();