我有2个问题。
第一个问题
我正在使用表格创建考试预约。我的问题是,当我创建变量并将其发送到视图时,只能通过它们的id
选择多个值。从integer
到string
似乎没有变化。
例如,如果显示为“ 1”,我想显示“ C1高级英语”。
Form.blade.php
<div class="form-group {{ $errors->has('exam_id') ? 'has-error' : '' }}">
<label for="exam_id" class="col-md-2 control-label">Exam</label>
<div class="col-md-10">
<select class="form-control" id="exam_id" name="exam_id">
<option value="" style="display: none;" {{ old('exam_id', optional($exams)->level ?: '') == '' ? 'selected' : '' }} disabled selected>Select exam</option>
@foreach ($exams as $key => $exam)
<option value="{{ $key }}" {{ old('exam_id', optional($exams)->level) == $key ? 'selected' : '' }}>
{{ $exam }}
</option>
@endforeach
</select>
{!! $errors->first('', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : '' }}">
<label for="user_id" class="col-md-2 control-label">User</label>
<div class="col-md-10">
<select class="form-control" id="user_id" name="user_id">
<option value="" style="display: none;" {{ old('user_id', optional($users)->name ?: '') == '' ? 'selected' : '' }} disabled selected>Select user</option>
@foreach ($users as $key => $user)
<option value="{{ $key }}" {{ old('user_id', optional($users)->name) == $key ? 'selected' : '' }}>
{{ $user }}
</option>
@endforeach
</select>
{!! $errors->first('user_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('booking_date') ? 'has-error' : '' }}">
<label for="booking_date" class="col-md-2 control-label">Booking Date</label>
<div class="col-md-10">
<select class="form-control" id="booking_date" name="booking_date">
<option value="" style="display: none;" {{ old('booking_date', optional($dateExams)->date ?: '') == '' ? 'selected' : '' }} disabled selected>Select booking date</option>
@foreach ($dateExams as $key => $dateExam)
<option value="{{ $key }}" {{ old('booking_date', optional($dateExams)->date) == $key ? 'selected' : '' }}>
{{ $dateExam}}
</option>
@endforeach
</select>
{!! $errors->first('booking_date', '<p class="help-block">:message</p>') !!}
</div>
</div>
ExamreservationController
class ExamReservationsController extends Controller
{
/**
* Display a listing of the exam reservations.
*
* @return Illuminate\View\View
*/
public function index()
{
$examReservations = Exam_reservation::with('exam','user','date_exam')->paginate(25);
return view('exam_reservations.index', compact('examReservations'));
}
/**
* Show the form for creating a new exam reservation.
*
* @return Illuminate\View\View
*/
public function create()
{
$exams = Exam::pluck('id','level')->all();
$users = User::pluck('name','id')->all();
$dateExams = Date_Exam::pluck('exam_id','date')->all();
return view('exam_reservations.create', compact('exams','users','dateExams'));
}
/**
* Store a new exam reservation in the storage.
*
* @param Illuminate\Http\Request $request
*
* @return Illuminate\Http\RedirectResponse | Illuminate\Routing\Redirector
*/
public function store(Request $request)
{
try {
$data = $this->getData($request);
Exam_reservation::create($data);
return redirect()->route('exam_reservations.index')
->with('success_message', 'Exam Reservation was successfully added.');
} catch (Exception $exception) {
return back()->withInput()
->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
}
}
/**
* Display the specified exam reservation.
*
* @param int $id
*
* @return Illuminate\View\View
*/
public function show($id)
{
$examReservation = Exam_reservation::with('exam','user')->findOrFail($id);
return view('exam_reservations.show', compact('examReservation'));
}
/**
* Show the form for editing the specified exam reservation.
*
* @param int $id
*
* @return Illuminate\View\View
*/
public function edit($id)
{
$examReservation = Exam_reservation::findOrFail($id);
$exams = Exam::pluck('id','id')->all();
$users = User::pluck('name','id')->all();
return view('exam_reservations.edit', compact('examReservation','exams','users'));
}
/**
* Update the specified exam reservation in the storage.
*
* @param int $id
* @param Illuminate\Http\Request $request
*
* @return Illuminate\Http\RedirectResponse | Illuminate\Routing\Redirector
*/
public function update($id, Request $request)
{
try {
$data = $this->getData($request);
$examReservation = Exam_reservation::findOrFail($id);
$examReservation->update($data);
return redirect()->route('exam_reservations.index')
->with('success_message', 'Exam Reservation was successfully updated.');
} catch (Exception $exception) {
return back()->withInput()
->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
}
}
/**
* Remove the specified exam reservation from the storage.
*
* @param int $id
*
* @return Illuminate\Http\RedirectResponse | Illuminate\Routing\Redirector
*/
public function destroy($id)
{
try {
$examReservation = Exam_reservation::findOrFail($id);
$examReservation->delete();
return redirect()->route('exam_reservations.index')
->with('success_message', 'Exam Reservation was successfully deleted.');
} catch (Exception $exception) {
return back()->withInput()
->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
}
}
/**
* Get the request's data from the request.
*
* @param Illuminate\Http\Request\Request $request
* @return array
*/
protected function getData(Request $request)
{
$rules = [
'exam_id' => 'string|required',
'user_id' => 'string|required',
'booking_date' => 'date|after:tomorrow|required',
'status' => 'string|min:1|nullable',
];
$data = $request->validate($rules);
return $data;
}
}
考试预订模型
class Exam_Reservation extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'exam_reservations';
/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'exam_id',
'user_id',
'booking_date',
'status'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
public function setBookingDateAttribute($value):void
{
$this->attributes['booking_date'] = Carbon::createFromFormat(config('app.date_format'), $value)->format('Y-m-d');
}
public function getBookingDateAttribute($value)
{
return Carbon::createFromFormat('Y-m-d', $value)->format(config('app.date_format'));
}
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Get the course for this model.
*
* @return App\Models\Course
*/
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function date_exam()
{
return $this->hasOneThrough(Date_Exam::class, Exam::class);
}
}
第二个问题
第三个输入是动态选择。我想知道是否可能只显示属于特定考试的日期。
该选择带有日期。有人知道如何在选择中威胁日期吗?
谢谢大家。
答案 0 :(得分:0)
第一个问题
您正在{{ $exam }}
标签内输出{{ $user }}
{{ $dateExam }}
和<option>
。您可能要指定要显示的模型的哪个属性,例如{{ $user->email }}
。
第二个问题
解决方案1:您可以将考试和日期下拉列表组合在一起,以便用户必须一次选择两者。
<option>Exam A on date 1</option>
<option>Exam A on date 2</option>
<option>Exam B on date 3</option>
解决方案2:将表单拆分为多个页面。在表格1中选择了考试,在页面2中选择了日期。
解决方案3:通过使用Javascript和AJAX请求选择考试后,加载日期。