在Laravel 8中从不同的控制器传递多个变量到单个视图



我需要从两个不同的控制器传递两个变量到一个视图,并在Laravel 8中显示来自数组变量的不同值到两个不同的html表单的选择选项。但是Laravel似乎不允许将两个不同的变量传递给单个视图。

我该如何解决这个问题?

请从这个链接找到错误截图。

涉及的控制器:TimeController, courecontroller

时间控制器:

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

路线:

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

视图:

@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

所以根据你的评论,我理解你的问题。而且你不需要两个控制器和单独的路由。

运行

创建名称为StudentEnrollmentController

的新控制器php artisan make:controller StudentEnrollmentController,并将以下代码粘贴到控制器

<?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'));
}
}

添加以下行到routes/web.php

Route::get('/enrollmentForm', [AppHttpControllersAppHttpControllers::class, 'showEnrollmentForm'])->name('enrollmentForm');

现在访问URLenrollmentForm,它将显示表单没有错误。

相关内容

  • 没有找到相关文章

最新更新