如何从条件为 laravel 的 3 个表中获取所有数据?

How to fetch all data from 3 tables with condition in laravel?

我有 3 table 所学校,schooldetails & admission,我想获取索引页中的所有数据我该怎么做,我正在获取学校和录取的数据 table 由于关系但不是学校的详细信息请检查并帮助

school

id
name
name
mobile
city


schooldetails
id
school_id(foreign key of school id)
contact_person

admission

id
school_id(foreign key of school_deatils id)
admission_classes
start_date
end_date

My function
public function schoolslist($class='', $city='')
        {
        $schools = Admission::where('admission_classes', 'like', "%{$class}%")->where('status', '1')->orderBy('id','desc')->whereHas('school', function($query) use($city) {
    $query->where('city', 'like', $city);
})->paginate(10);
          return view('frontend.index',compact('schools'));

        }
        my Admission model
        public function School()
    {
        return $this->belongsTo('App\School');
    }
    public function SchoolDetails()
    {
        return $this->belongsTo('App\SchoolDetails');
    }
        
        My view
         @foreach($schools as $i => $school)
         {{ $school->School->name}} from user table Ravi
       {{ $school->SchoolDetails->contact_person}} //from school_Details table  No result 
         {{ $school->start_date }} //from admission table  date 11/22/2018
         @enforeach

SchoolDetails 关系应该与学校而非 addmission 相关,因此从 Admission 模型中删除 SchoolDetails() 并将其添加到学校模型

学校模型:

public function Admission()
{
    return $this->hasMany('App\Admission');    
}
public function SchoolDetails()
{
    return $this->hasOne('App\SchoolDetails');    \Edited
}

录取模式:

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

学校详情模型:

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

然后您可以相应地更改视图:

@foreach($schools as $i => $school)
    {{ $school->School->name}} from user table Ravi
    {{ $school->School->SchoolDetails->contact_person}}  // This Line is changed
    {{ $school->start_date }}
@enforeach