Skip to content
Advertisement

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. khatriram55@gmail.com">
            </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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement