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
4
  • 3
    Can you please format your code a bit more? It's really hard to follow with all the > and -\> Commented Jan 28, 2023 at 11:54
  • I apologize for being so carelessly written. This is the first time I'm asking a question on the site Commented Jan 28, 2023 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. Commented Jan 28, 2023 at 12:45
  • Does this answer your question? How to pass data to view in Laravel? Commented Jul 12, 2023 at 16:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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
        ]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.