保存新的Eloquent模型时,Laravel 5.3 - 500内部错误

时间:2016-10-09 16:12:29

标签: laravel eloquent

尝试创建模型Apartment的新实例时,我得到500 (Internal Server Error)。我有其他/create/路线用于创建我的模型的新实例,它们正常工作,并且它们设置与ApartmentController@create相同,但由于某些奇怪的原因,它不会工作

ApartmentController@create

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $addresses = Address::all();
    return view('address/create')->with('addresses', $addresses);
}

ApartmentController@store

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $address = new Address();
    $this->authorize('create', $address);

    $validator = Validator::make($request->all(), [
        'building_number' => 'required|integer',
        'street' => 'required|string',
    ]);

    if ($validator->fails()) {
        return redirect('addresses/create')
            ->withErrors($validator)
            ->withInput();
    }

    $address->building_number = $request->building_number;
    $address->street = $request->street;

    $address->save();

    return redirect('addresses/create');
}

View apartment/create

<div class="col-md-6">
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <form class="form-horizontal" method="POST" action="{{ action("ApartmentController@store") }}">
            <fieldset>
                <!-- Form Name -->
                <legend>Register a new apartment</legend>

                {{ csrf_field() }}

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="address_id">Address</label>
                    <div class="col-md-4">
                        <select id="address_id" name="address_id" class="form-control">
                            @foreach($addresses as $address)
                                <option value="{{$address->id}}">{{$address->street}} {{$address->building_number}}</option>
                            @endforeach
                        </select>
                        <span class="help-block">Apartments building number</span>
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="apartment_number">Apartment Number</label>
                    <div class="col-md-4">
                        <input id="apartment_number" name="apartment_number" type="text" placeholder="2402" class="form-control input-md" required="">
                        <span class="help-block">The apartment number</span>
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="size">Apartment Size</label>
                    <div class="col-md-4">
                        <input id="size" name="size" type="text" placeholder="56qm" class="form-control input-md" required="">
                        <span class="help-block">The size of the apartment in qm</span>
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="living_area_size">Living Area Size</label>
                    <div class="col-md-4">
                        <input id="living_area_size" name="living_area_size" type="text" placeholder="23qm" class="form-control input-md" required="">
                        <span class="help-block">The size of the living area in qm</span>
                    </div>
                </div>

                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="suitable_for">Suitable For</label>
                    <div class="col-md-4">
                        <select id="suitable_for" name="suitable_for" class="form-control">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                        </select>
                        <span class="help-block">Number of people the apartment is suitable for.</span>
                    </div>
                </div>

                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="furnished">Furnished</label>
                    <div class="col-md-4">
                        <select id="furnished" name="furnished" class="form-control">
                            <option value="1">Yes</option>
                            <option value="0">No</option>
                        </select>
                    </div>
                </div>

                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="balcony_or_terrace">Balcony or Terrace</label>
                    <div class="col-md-4">
                        <select id="balcony_or_terrace" name="balcony_or_terrace" class="form-control">
                            <option value="none">None</option>
                            <option value="balcony">Balcony</option>
                            <option value="terrace">Terrace</option>
                        </select>
                    </div>
                </div>

                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="floor">Floor</label>
                    <div class="col-md-4">
                        <select id="floor" name="floor" class="form-control">
                            <option value="ground">Ground</option>
                            <option value="first">First</option>
                            <option value="second">Second</option>
                            <option value="third">Third</option>
                        </select>
                    </div>
                </div>

                <!-- Textarea -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="description">Description</label>
                    <div class="col-md-6">
                        <textarea class="form-control" id="description" name="description"></textarea>
                    </div>
                </div>

                <!-- Button -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="submit"></label>
                    <div class="col-md-4">
                        <button id="submit" name="submit" type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </div>

            </fieldset>
        </form>
    </div>
    <div class="col-md-4">
        <legend>Registered Apartments</legend>
        @foreach($apartments as $apartment)
            <div class="address">
                Apartment {{ $apartment->apartment_number }}

                <a href="{{ url('/apartments/' . $apartment->id . '/edit') }}"
                   onclick="event.preventDefault();
                           document.getElementById('edit-form-{{$apartment->id}}').submit();">
                    <button class="btn btn-default">Edit</button>
                </a>

                <form id="edit-form-{{$apartment->id}}" action="{{ url('/apartments/' . $apartment->id . '/edit') }}" method="GET" style="display: none;">
                    {{ csrf_field() }}
                    <input type="hidden" name="address_id" value="{{$apartment->id}}">
                </form>


                <a href="{{ url('/apartments/' . $apartment->id) }}"
                   onclick="event.preventDefault();
                           document.getElementById('delete-form-{{$apartment->id}}').submit();">
                    <button class="btn btn-warning">Delete</button>
                </a>

                <form id="delete-form-{{$apartment->id}}" method="POST" action="/apartments/{{$apartment->id}}">
                    {{ csrf_field() }}
                    {{ method_field('DELETE') }}
                </form>
            </div>
            <br>
        @endforeach
    </div>

Route web.php

Route::resource('apartments', 'ApartmentController');

公寓模型的公寓表 apartments table

我尝试在save()方法上捕获异常,但没有捕获任何内容,我仍然被重定向到/apartments而没有任何Laravel错误只是Chroms 500错误页面。

这就是我尝试使用dwightwatson/validating捕获错误的方法:

try{
        $apartment = new Apartment();

        $apartment->apartment_number = $request->apartment_number;
        $apartment->size = $request->size;
        $apartment->living_area_size = $request->living_area_size;
        $apartment->suitable_for = $request->suitable_for;
        $apartment->furnished = $request->furnished;
        $apartment->balcony_or_terrace = $request->balcony_or_terrace;
        $apartment->floor = $request->floor;
        $apartment->description = $request->description;
        $apartment->address_id = $request->address_id;

        $apartment->saveOrFail();

    } catch (ValidationException $e) {
        $errors = $e->getErrors();

        return redirect()->route('apartments.create')
        ->withErrors($errors)
        ->withInput();
    }

1 个答案:

答案 0 :(得分:0)

首先,如果您还没有,请Download Fiddler。将其启动,然后再次触发存储方法。提琴手应该告诉你很多关于你的要求和回应的信息:

Fiddler Web Debugger

在左侧,您有一个请求列表,在右侧,您有统计信息和检查员。在检查员标签下,您会看到 WebView 。单击它,您将看到Laravel生成的错误。

至于它可能失败的原因,请检查引用是否正确包含在内。 e.g:

use App\Address;