Home > other >  laravel controller returns only 2 values
laravel controller returns only 2 values

Time:01-29

I have a database with 3 tables. A separate model is connected to each table, and there is a controller that accepts values from all models. On the site page, I will have 3 tables that will be populated from a mysql table. When I connected 2 models, everything worked fine. But after connecting 3, I get an error

undefined variable: sec_3.

If you delete one of the variables, then everything will work fine. It seems to me that the problem is either with the controller or with the file blade.php but I do not know how to fix it so that everything works properly. How to fix it?

My code: Controller:

class PreschoolInstitution3Controller extends Controller {
    public function index(){
        $context=['bbs' =>PreschoolInstitution3::latest()->get()]; 
        $context_2=['s' =>PreschoolInstitution::latest()->get()]; 
        $context_3=['sec_3' => TrainingPrograms::latest()->get()]; 
        return view('our_employees', $context, $context_2, $context_3); 
    }
}

web.php:

Route::get('/OurEmployees',[PreschoolInstitution3Controller::class,'index'] )->name('OurEmployees');

blade.php:

@foreach ($s as $section_2) <tr> <td>{{$section_2->number}}<td> <td>{{$section_2->fullname }}<td> <td>{{$section_2->post }}<td> <td>{{$section_2->telephone }}</td> <td>{{$section_2->email }}</td>
    @endforeach @foreach ($bbs as $section )

  {{$section->number}}   {{$section->full_name}}   {{$section->post}}   {{$section->education}}   {{$section->category}}   {{$section->teaching_experience}}   {{$section->professional_development}}

  @endforeach @foreach ($sec_3 as $section_3)

      {{ $section_3->number }}
      {{ $section_3->level }}
      {{ $section_3->directions }}
      {{ $section_3->type_of_educational_program }}
      {{ $section_3->period_of_assimilation }}
      {{ $section_3->number_of_students }}

    @endforeach

CodePudding user response:

You should pass an array of data to view:

class PreschoolInstitution3Controller extends Controller {
    public function index(){
        $context = [
           'bbs' => PreschoolInstitution3::latest()->get(),
           's' => PreschoolInstitution::latest()->get(),
           'sec_3' => TrainingPrograms::latest()->get()
        ];
        return view('our_employees', $context); 
    }
}

https://laravel.com/docs/9.x/views#passing-data-to-views

  • Related