未定义的变量:在Laravel中

时间:2019-11-21 22:33:45

标签: php laravel eloquent

我无法在{!! Form::open(['action' => ['SeatController@book', $seat->id] , 'method' => 'POST'])!!}的表单操作中获得可变席位。这是我的代码:

blade.php:

@extends('layouts.app')

@section('content')
    <br><br><br>
    <h3>From {{$bus->departdest}} to {{$bus->arrivedest}}</h3>
    <div> Date : {{$bus->date}}</div>
    <div> Boarding Time : {{$bus->depart}}</div>
    <div> Arrival Time : {{$bus->arrival}}</div>
    <div> Plate Number : {{$bus->platenum}}</div>
    <hr>

 <img src="/images/seat.jpg" height="175.5px" width="521.5px" >
 <hr>
    <div> <h3>Available Seats : {{$bus->available}} </h3></div>
    @if ($bus->available>0)
    {!! Form::open(['action' => ['SeatController@book', $seat->id] , 'method' => 'POST'])!!}
    @csrf
        <div class="form-group row">
            <label for="num" class="col-sm-4 col-form-label text-md-right">{{ __('Choose seat') }}</label>
            <div class="col-md-12">
                <select class="form-control{{ $errors->has('num') ? ' is-invalid' : '' }}" name="num">
                @foreach($seats as $seat)
                    @if ($seat->bus_id == $bus->id && $seat->status == 0)
                    <option value="{{ $seat->id }}">{{ $seat->num }}</option>
                    @endif
                @endforeach
                </select>

                @if ($errors->has('num'))
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $errors->first('num') }}</strong>
                    </span>
                @endif
            </div>
        </div>
        <br>
        {{Form::hidden('_method','POST')}}
        {{Form::submit('Confirm', ['class' => 'btn btn-primary'])}}
        <a href="/search" class="btn btn-primary"> Cancel</a>
    {!! Form::close() !!}
    @else
    <div> No available ticket </div>
    <a href="/search" class="btn btn-primary"> Cancel</a>
    @endif

@endsection

BusController.php:

    public function show($id){
    $bus = Bus::find($id);
    $user = Auth::user();
    $seats = Seat::all();
    return view('showseat')->with(compact('bus', 'user', 'seats'));
}

1 个答案:

答案 0 :(得分:0)

您可能需要在公共汽车上预订座位?所以在这里:

['SeatController@book', $seat->id]

应该是:

['SeatController@book', $bus->id]

在将$seat传递到视图时,您不能在那里使用$seats,并且要在select元素中反复显示座位号。

然后在您的book函数中,您仍然可以使用来获取座位号

$request->num // or request('num')

由于num是席位号选择字段的name属性。