dimanche 23 octobre 2016

laravel 5.3 : Undefined property: 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></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\se\resources\views\pages\welcome.blade.php)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire