简体   繁体   English

未定义变量 $booking 在视图中(Laravel 5.7)

[英]Undefined variable $booking in view (Laravel 5.7)

So I am working on a simple CRUD.所以我正在研究一个简单的 CRUD。

I organize my form in a separate page view so I can just insert it on my create and edit views.我在一个单独的页面视图中组织我的表单,这样我就可以将它插入到我的创建和编辑视图中。

Here's how my field view:这是我的现场视图:

<div class="form-group row">
        <label class="col-sm-2 col-form-label"for="room_id">Room</label>
        <div class="col-sm-10">
            <select name="room_id" class="form-control" id="room_id" required>
                @foreach($rooms as $id => $display)
                    <option value="{{ $id }}" {{ (isset($booking->room_id) && $id === $booking->room_id) ? 'selected' : '' }}>{{ $display }}</option>
                @endforeach
            </select>
            <small class="form-text text-muted">The room number being booked.</small>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-sm-2 col-form-label"for="user_id">User</label>
        <div class="col-sm-10">
            <select name="user_id" class="form-control" id="user_id" required>
                @foreach($users as $id => $display)
                    <option value="{{ $id }}" {{ (isset($bookingsUser->user_id) && $id === $bookingsUser->user_id) ? 'selected' : '' }}>{{ $display }}</option>
                @endforeach
            </select>
            <small class="form-text text-muted">The user booking the room.</small>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-sm-2 col-form-label" for="start">Start Date</label>
        <div class="col-sm-10">
            <input name="start" type="date" class="form-control" required placeholder="yyyy-mm-dd" value="{{ $booking->start ?? '' }}"/>
            <small class="form-text text-muted">The start date for the booking.</small>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-sm-2 col-form-label" for="start">End Date</label>
        <div class="col-sm-10">
            <input name="end" type="date" class="form-control" required placeholder="yyyy-mm-dd" value="{{ $booking->end ?? '' }}"/>
            <small class="form-text text-muted">The end date for the booking.</small>
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-2">Paid Options</div>
        <div class="col-sm-10">
            <div class="form-check">
                <input name="is_paid" type="checkbox" class="form-check-input" value="1" {{ $booking->is_paid ? 'checked' : '' }}/>
                <label class="form-check-label" for="start">Pre-Paid</label>
                <small class="form-text text-muted">If the booking is being pre-paid.</small>
            </div>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-sm-2 col-form-label" for="notes">Notes</label>
        <div class="col-sm-10">
            <input name="notes" type="text" class="form-control" placeholder="Notes" value="{{ $booking->notes ?? '' }}"/>
            <small class="form-text text-muted">Any notes for the booking.</small>
        </div>
    </div>

Now this line gets the attention:现在这一行引起了注意:

  <input name="is_paid" type="checkbox" class="form-check-input" value="1" {{ $booking->is_paid ? 'checked' : '' }}/>

When I access my create view it says:当我访问我的创建视图时,它说:

Undefined variable: booking (View: /home/vagrant/code/resources/views/bookings/fields.blade.php) (View: /home/vagrant/code/resources/views/bookings/fields.blade.php)

Looking at the code the $booking variable is working on other form field items but on this particular item it doesnt.查看代码 $booking 变量正在处理其他表单字段项目,但在此特定项目上却没有。

Any idea what I am missing?知道我缺少什么吗?

EDIT: Here's the controller:编辑:这是控制器:

<?php

namespace App\Http\Controllers;

use App\Booking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BookingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
       //  \DB::table('bookings')->get()->dd();
       $bookings = DB::table('bookings')->get();
       return view('bookings.index')
       ->with('bookings', $bookings);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        // $users = DB::table('users')->get()->pluck('name', 'id')->dd();
        $users = DB::table('users')->get()->pluck('name', 'id')->prepend('none');
        $rooms = DB::table('rooms')->get()->pluck('number', 'id');
        return view('bookings.create')
        ->with('users', $users)
        ->with('rooms', $rooms);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
       //  dd($request->all());
        $id = DB::table('bookings')->insertGetId([
            'room_id' => $request->input('room_id'),
            'start' => $request->input('start'),
            'end' => $request->input('end'),
            'is_reservation' => $request->input('is_reservation', false),
            'is_paid' => $request->input('is_paid', false),
            'notes' => $request->input('notes'),
        ]);
        DB::table('bookings_users')->insert([
            'booking_id' => $id,
            'user_id' => $request->input('user_id'),
        ]);
        return redirect()->action('BookingController@index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function show(Booking $booking)
    {
        //
        // dd($booking);
        return view('bookings.show', ['booking' => $booking]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function edit(Booking $booking)
    {
        //
        $users = DB::table('users')->get()->pluck('name', 'id')->prepend('none');
        $rooms = DB::table('rooms')->get()->pluck('number', 'id');
        $bookingsUser = DB::table('bookings_users')->where('booking_id', $booking->id)->first();
        return view('bookings.edit')
            ->with('bookingsUser', $bookingsUser)
            ->with('users', $users)
            ->with('rooms', $rooms)
            ->with('booking', $booking);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Booking $booking)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Booking  $booking
     * @return \Illuminate\Http\Response
     */
    public function destroy(Booking $booking)
    {
        //
    }
}

您可以添加isset来检查三元运算,

{{ isset($booking->is_paid) ? 'checked' : '' }}

The reason is that you are passing $bookings throught the controller and using it as $booking in the view.原因是您通过控制器传递$bookings并将其用作视图中的$booking So what you can do is either change the controller variable as $booking or change the varibale in the view to $booking .因此,您可以做的是将控制器变量更改为$booking或将视图中的变量更改为$booking As I felt its easy to change the controller variable.因为我觉得更改控制器变量很容易。

 public function index()
{
    //
   //  \DB::table('bookings')->get()->dd();
   $bookings = DB::table('bookings')->get();
   return view('bookings.index')
   ->with('booking', $bookings);

}

What I did here is just changed the name of the variable that you pass to view as ->with('booking',我在这里所做的只是更改了您传递给视图的变量的名称->with('booking',

Change the input line for the checkbox to read:将复选框的输入行更改为:

    <input type="checkbox" class="form-check-input" name="is_paid" value="1"
    {{(isset($booking->is_paid)) && $booking->is_paid == 1 ? 'checked ' : ' '}}/>

Create, show, and edit should all work.创建、显示和编辑应该都能正常工作。

Why you are not passing booking in your create method?为什么你没有在你的创建方法中通过预订? But you are doing it in the edit method.但是您是在编辑方法中进行的。 If they are using the same view then you should pass the booking object from both of those actions or either way you can use isset to avoid php throw an exception.如果他们使用相同的视图,那么您应该从这两个操作中传递预订对象,或者您可以使用 isset 来避免 php 抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM