laravel 5.3:未定义的属性:Illuminate \ Database \ Eloquent \ Collection Error

I have one table called doctor (staff_id,specialization) and another table is staff (staff_id,password,name,email,address,department,sub_department,post) and in doctor table foreign key staff_id is references to staff table's staff_id attribute. I don't know Why I got this error I am using Laravel 5.3 please help me.

here is my "Staff Model"

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Staff extends Model
    {
       protected $table = 'Staff';

       public function doctor(){
          return $this->hasOne('App\Doctor');   
       }
    }

here is my "Doctor Model"

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Doctor extends Model
    {
       protected $table = 'Doctor';
       public function staff(){
           return $this->belongsTo('App\Staff');
       }
    }

This is Controller

    use App\Doctor;

    use App\Staff;

    class PageController extends Controller
    {
        public function getIndex(){
           $doctor = Doctor::all();
           return view('pages.welcome')->withDoctor($doctor);
        }
   }

And This is my welcome.blade.php where I use it

 <pre>
   <tr>
    <td>{{ $doctor->staff->name }}</td>
   </tr>
 </pre>

and when I run it I got this error:

ErrorException in 150a6b9d586ccbe6225ed748a7eb8dfc842ce323.php line 26: Undefined property: Illuminate\Database\Eloquent\Collection::$staff (View: C:\wamp\www\seesources\views\pages\welcome.blade.php)

Try passing the data from your controller like this:

public function getIndex(){
    $doctor = Doctor::all();
    return view('pages.welcome')->with('doctor', $doctor);
}
Your controller should be.
    use App\Doctor;

    use App\Staff;

    class PageController extends Controller
    {
        public function getIndex(){
           $doctor = Doctor::all();
           return view('pages.welcome',compact('doctor'));
   }
}

Your views should be-

 <pre>
@foreach($doctor as $doctors)
   <tr>
    <td>{{ $doctors->name }}</td>
   </tr>
@endforeach
 </pre>

thank you guys for your help I finally found out how to solve this error here are some points which can help you - 1. In Model we should tell model a primary key like my "STAFF" model will be like namespace App;

use Illuminate\Database\Eloquent\Model;

class Staff extends Model
{
   protected $primarykey = 'staff_id';
   protected $table = 'Staff';

   public function doctor(){
      return $this->hasOne('App\Doctor');   
   }
}

now we can default call primary key as "id".

we have to change this for our "DOCTOR" model too namespace App;

use Illuminate\Database\Eloquent\Model;

class Doctor extends Model
{
   protected $primarykey = 'staff_id';
   protected $table = 'Doctor';
   public function staff(){
       return $this->belongsTo('App\Staff');
   }
}
  1. now our controller should be like

    use App\Doctor;

    use App\Staff;

    class PageController extends Controller { public function getIndex(){ $doctor = Doctor::find(id); return view('pages.welcome')->withDoctor($doctor); } }

  2. now in my welcome.blade.php should be like -

      
       {{ $doctor->staff->name }}
      
     

and this is working.

for more information click here