Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raviawasti's avatar

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

I have 3 tables school ,schooldetails & admission(school have multiple table), and i want to get all data in index page how can i do that , i am getting data of school and admission table due to relationship but not of school details kindly check it and help

school (Model name is School but i am saving data to users table by using ptotected table="users")

id name name mobile city

schooldetails(School can have only one deatils) id school_id(foreign key of school id) contact_person

admission(School can have multiple 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
0 likes
6 replies
Vilfago's avatar

IF everything work, but it just miss SchoolDetails load it with with()

$schools = Admission::with('SchoolDetails')->where('admission_classes', 'like', "%{$class}%")->where('status', '1')->orderBy('id','desc')->whereHas('school', function($query) use($city) { $query->where('city', 'like', $city); })->paginate(10);
salomon022's avatar
$users = DB::table('school')
            ->join('schooldetails', 'schooldetails.school_id', '=', 'school.id')
            ->join('admission', 'admission.school_id', '=', 'school.id')
            ->get();
raviawasti's avatar

@VILFAGO - Call to undefined method Illuminate\Database\Query\Builder::SchoolDetails()

  public function schoolslist($class='', $city='')

    {

$schools = Admission::with('SchoolDetails')->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'));

    }
Vilfago's avatar

Did you really have this in your Admission model ?

public function SchoolDetails()
{
    return $this->belongsTo('App\SchoolDetails');
}
raviawasti's avatar

Yes i had added it just now, i am getting result for admission table with this {{ $school->admission_end_date }} and school details from this {{ $school->School['school_name']}} but when i try to fetch school detail {{$school->SchoolDetails['slug']} from this i m getting nothing

raviawasti's avatar

@VILFAGO - Yes i had added it just now, i am getting result for admission table with this {{ $school->admission_end_date }} and school details from this {{ $school->School['school_name']}} but when i try to fetch school detail {{$school->SchoolDetails['slug']} from this i m getting nothing

Please or to participate in this conversation.