Filter the data of a school to which the user belongs

63 Views Asked by At

I want to display the list of students from a school the user belongs to and not display data from other schools. for that I created two user tables: id , school_id school table: id, name, address,.... student table: id,school_id,name,address,.... I want to display the students of the school to whom this user belongs the display is done in browse.blade.php generated by voyageur how can i make this filter?

the user belongs to a company and can only see data from that school I have a browse in /views/students that shows students from all schools

1

There are 1 best solutions below

0
Karl Hill On

Controller

use App\Models\User;
use App\Models\Student; // Assuming you have a Student model

public function browseStudents()
{
    $user = auth()->user();
    $schoolId = $user->school_id;
    $students = Student::where('school_id', $schoolId)->get();


    return view('students.browse', compact('students'));
}

View

@foreach ($students as $student)
    <p>{{ $student->name }} - {{ $student->address }}</p>
@endforeach