简体   繁体   English

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

[英]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. 我有一个表,称为Doctor(staff_id,specialization),另一张表是staff(staff_id,password,名称,电子邮件,地址,部门,sub_department,post),在医生表中,外键staff_id是对职员表的staff_id属性的staff_id I don't know Why I got this error I am using Laravel 5.3 please help me. 我不知道为什么我使用Laravel 5.3出现此错误,请帮帮我。

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 这是我使用它的welcome.blade.php

 <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\\se\\resources\\views\\pages\\welcome.blade.php) 150a6b9d586ccbe6225ed748a7eb8dfc842ce323.php第26行中的ErrorException:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ staff(视图:C:\\ wamp \\ www \\ se \\ resources \\ 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; 谢谢大家的帮助,我终于找到了解决该错误的方法,这里有一些问题可以帮助您-1。在模型中,我们应该告诉模型主键,例如“ STAFF”模型将类似于命名空间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". 现在我们可以默认将主键称为“ id”。

we have to change this for our "DOCTOR" model too namespace App; 我们也必须为“ DOCTOR”模型更改此名称空间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; 使用App \\ Doctor;

    use App\\Staff; 使用App \\ Staff;

    class PageController extends Controller { public function getIndex(){ $doctor = Doctor::find(id); 类PageController扩展了控制器{公共函数getIndex(){$ doctor = Doctor :: find(id); return view('pages.welcome')->withDoctor($doctor); 返回视图('pages.welcome')-> withDoctor($ doctor); } } }}

  2. now in my welcome.blade.php should be like - 现在在我的welcome.blade.php中应该像-

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

and this is working. 这正在工作。

for more information click here 欲了解更多信息,请点击这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3 - Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3 Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 PHP错误:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ retails - PHP error: Undefined property: Illuminate\Database\Eloquent\Collection::$retails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM