0

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
miken32
  • 42,008
  • 16
  • 111
  • 154
Kotomon
  • 39
  • 3
  • 3
    Can you please format your code a bit more? It's really hard to follow with all the `>` and `-\>` – apokryfos Jan 28 '23 at 11:54
  • I apologize for being so carelessly written. This is the first time I'm asking a question on the site – Kotomon Jan 28 '23 at 12:36
  • I'm not trying to offend or anything like that. It's just that a better formatted question means more likely for someone to read and understand the question and provide an answer if they have one. – apokryfos Jan 28 '23 at 12:45
  • Does this answer your question? [How to pass data to view in Laravel?](https://stackoverflow.com/questions/18341792/how-to-pass-data-to-view-in-laravel) – miken32 Jul 12 '23 at 16:34

2 Answers2

0

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

Wilson
  • 755
  • 4
  • 12
0

Another one is that add a second parameter of an array with name to view( ) .

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