【发布时间】:2017-03-05 14:55:08
【问题描述】:
我有一张表叫医生(staff_id,specialization),另一张表是员工(staff_id,password,name,email,address,department,sub_department,post),在医生表中,外键 staff_id 是对员工表的引用staff_id 属性。我不知道为什么我使用 Laravel 5.3 时出现此错误,请帮助我。
这是我的“员工模型”
namespace App;
use Illuminate\Database\Eloquent\Model;
class Staff extends Model
{
protected $table = 'Staff';
public function doctor(){
return $this->hasOne('App\Doctor');
}
}
这是我的“医生模型”
namespace App;
use Illuminate\Database\Eloquent\Model;
class Doctor extends Model
{
protected $table = 'Doctor';
public function staff(){
return $this->belongsTo('App\Staff');
}
}
这是控制器
use App\Doctor;
use App\Staff;
class PageController extends Controller
{
public function getIndex(){
$doctor = Doctor::all();
return view('pages.welcome')->withDoctor($doctor);
}
}
这是我的welcome.blade.php 我使用它的地方
<pre>
<tr>
<td>{{ $doctor->staff->name }}</td>
</tr>
</pre>
当我运行它时,我得到了这个错误:
150a6b9d586ccbe6225ed748a7eb8dfc842ce323.php 第 26 行中的错误异常: 未定义属性:Illuminate\Database\Eloquent\Collection::$staff(查看: C:\wamp\www\se\resources\views\pages\welcome.blade.php)
【问题讨论】:
标签: php mysql laravel-5 laravel-5.3