Passing more than one variables from different controllers to a single view in Laravel 8

I need to pass two variables from two different controllers to a view and display the different values from the array variables to two different html form’s select options in Laravel 8. But Laravel doesn’t seem to allow pass two differnt variable to a single view.

How do I slove this?

Please find the error screenshot from this link.

Controllers involved: TimeController, CourseController

Time Controller:

    <?php

namespace AppHttpControllers;

use AppModelsTime;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

class TimeController extends Controller
{
    public function loadTime()
    {
        $timeData = DB::table('time')->select('*')->get();
        return view('admin.forms.enrollment', ['timeList' => $timeData]);
    }
}

CourseController:
<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

class CourseController extends Controller
{
    public function loadCourses()
    {
        $courseData = DB::table('courses')->select('*')->get();
        return view('admin.forms.enrollment', ['courseList' => $courseData]);

        // $timeData = DB::table('time')->select('*')->get();
        // return view('admin.forms.enrollment', ['timeList' => $timeData]);
    }
}

Routes:
Route::get('/enroll', [TimeController::class, 'loadTime'])->name('loadTime');
Route::get('/enroll', [CourseController::class, 'loadCourses'])->name('loadCourses');

View:
@extends('admin.forms.layout')

@section('form')

<div class="form-group row">
          <div class="col-sm-4">
            <label for="lastname" class="col-sm-6 col-form-label">Username</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="uname" placeholder="eg. Khatri0b3d020d">
            </div>
          </div>
          <div class="col-sm-4">
            <label for="course" class="col-sm-6 col-form-label">Course</label>
            <div class="col-sm-10">
              <select name="course" id="course" class="form-control">
              
            @foreach($courseList as $course)

                <option value="{{$course->vehicle_category}}">{{$course->course_type}}</option>

              </select>
            </div>
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-4">
            <label for="email" class="col-sm-6 col-form-label">Package</label>
            <div class="col-sm-10">
              <input type="email" class="form-control" id="email" placeholder="eg. <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a01020b1e1803180b075f5f2a0d070b030644090507">[email protected]</a>">
            </div>
          </div>
          <div class="col-sm-4">
            <label for="phone" class="col-sm-6 col-form-label" value="+977">Start Date</label>
            <div class="col-sm-10">
              <input type="date" class="form-control" id="date" min="{{date('Y-m-d')}}">
            </div>
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-4">
            <label for="time" class="col-sm-6 col-form-label">Preferred Time</label>
            <div class="col-sm-10">
              <select name="time" id="time" class="form-control">
                <option selected default>Select your preferred time</option>
                
                @foreach($timeList as $time)

                <option value="{{$time->id}}">{{$time->time}}</option>

                @endforeach 

              </select>
            </div>
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-4">
            <input type="submit" value="Admit" name="admit" class="btn btn-success">
          </div>
</div>

@endsection

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

So based on your comment I understand your issue. And you don’t need Two controllers and seperate routes.

Create a New controller with name of StudentEnrollmentController by running

php artisan make:controller StudentEnrollmentController and paste the below code into controller

<?php

namespace AppHttpControllers;

use AppModelsTime;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

class StudentEnrollmentController extends Controller
{
    public function showEnrollmentForm()
    {
        $timeData = DB::table('time')->get();
        $courseData = DB::table('courses')->get();
        return view('admin.forms.enrollment', compact('timeData', 'courseData'));
    }
}

Add following line to routes/web.php
Route::get('/enrollmentForm', [AppHttpControllersAppHttpControllers::class, 'showEnrollmentForm'])->name('enrollmentForm');

Now visit the URL enrollmentForm it will Display the form without errors.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x