Php 如何更新laravel中的现有产品?

Php 如何更新laravel中的现有产品?,php,html,laravel,Php,Html,Laravel,我正在尝试使用以下方法在我的laravel应用程序中编辑现有属性的列表 列表控制器 public function update(Request $request, $id) { $listing = Listing::where('id', $id)->first(); $listing->title = $request->get('title'); $listing->price = $reques

我正在尝试使用以下方法在我的laravel应用程序中编辑现有属性的列表

列表控制器

    public function update(Request $request, $id)
    {

        $listing = Listing::where('id', $id)->first();

        $listing->title = $request->get('title');
        $listing->price = $request->get('price');
        $listing->address = $request->get('address');
        $listing->rooms = $request->get('rooms');
        $listing->city = $request->get('city');
        $listing->state = $request->get('state');
        $listing->zip_code = $request->get('zip_code');
        $listing->area = $request->get('area');
        $listing->balcony = $request->get('balcony');
        $listing->bedrooms = $request->get('bedrooms');
        $listing->bathrooms = $request->get('bathrooms');
        $listing->toilet = $request->get('toilet');
        $listing->bathroom_type = $request->get('bathroom_type');
        $listing->kitchen = $request->get('kitchen');
        $listing->parking_space = $request->get('parking_space');
        $listing->description = $request->get('description');
        $listing->featured = $request->get('featured');
        $listing->status = $request->get('status');
        $listing->type = $request->get('type');
        $listing->water_supply = $request->get('water_supply');
        $listing->power_supply = $request->get('power_supply');
        $listing->save();

        return redirect('/home')->with('success', 'Listing updated!');
    }
edit.blade.php

@extends('../layouts/home')

@section('content')

<!-- Titlebar
================================================== -->
<div id="titlebar" class="submit-page">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h2><i class="fa fa-plus-circle"></i> Add Property</h2>
            </div>
        </div>
    </div>
</div>


<!-- Content
================================================== -->
<div class="container">
<div class="row">

    <!-- Submit Page -->
    <div class="col-md-12">
        @if ($errors->any())

            <div class="notification notice large margin-bottom-55">
                 @foreach ($errors->all() as $error)
                    <li>{{$error}}</li>
                 @endforeach

                @if(session()->has('message'))
                    <div class="alert alert-success">
                        {{ session()->get('message') }}
                    </div>
                @endif

                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif
            </div>
        @endif
        <form action="{{ route('listings.update', $listing->id) }}" method="post" enctype="multipart/form-data">
            @method('PATCH')
            @csrf
            <div class="submit-page">

            <!-- Section -->
            <h3>Basic Information</h3>
            <div class="submit-section">


                <!-- Title -->
                <div class="form">
                    <h5>Property Title <i class="tip" data-tip-content="Type title that will also contains an unique feature of your property (e.g. renovated, air contidioned)"></i></h5>
                    <input class="search-field" type="text" value="{{ $listing->title }}" name="title" />
                    @if($errors->has('title'))
                    <span class="help-block text-danger">{{ $errors->first('title') }}</span>
                    @endif
                </div>

                <!-- Row -->
                <div class="row with-forms">

                    <!-- Status -->
                    <div class="col-md-4">
                        <h5>Status</h5>
                        <select class="chosen-select-no-single" name="status">
                            <option label="blank"></option>
                            <option>For Sale</option>
                            <option>For Rent</option>
                        </select>
                        @if($errors->has('status'))
                        <span class="help-block text-danger">{{ $errors->first('status') }}</span>
                        @endif
                    </div>

                    <!-- Type -->
                    <div class="col-md-4">
                        <h5>Type</h5>
                        <select class="chosen-select-no-single" name="type">
                            <option label="blank"></option>
                            <option>Apartment</option>
                            <option>House</option>
                            <option>Commercial</option>
                            <option>Garage</option>
                            <option>Lot</option>
                        </select>
                        @if($errors->has('type'))
                        <span class="help-block text-danger">{{ $errors->first('type') }}</span>
                        @endif
                    </div>

                    <div class="col-md-4">
                        <h5>Featured<span></span></h5>
                        <select class="chosen-select-no-single" name="featured">
                            <option label="blank"></option>
                            <option value="1">Yes</option>
                            <option value="0">No</option>
                        </select>
                        @if($errors->has('featured'))
                        <span class="help-block text-danger">{{ $errors->first('featured') }}</span>
                        @endif
                    </div>

                </div>
                <!-- Row / End -->


                <!-- Row -->
                <div class="row with-forms">

                    <!-- Price -->
                    <div class="col-md-4">
                        <h5>Price <i class="tip" data-tip-content="Type overall or monthly price if property is for rent"></i></h5>
                        <div class="select-input disabled-first-option">
                            <input type="text" data-unit="NGN" name="price" value="{{ $listing->username }}">
                        </div>
                        @if($errors->has('price'))
                        <span class="help-block text-danger">{{ $errors->first('price') }}</span>
                        @endif
                    </div>

                    <!-- Area -->
                    <div class="col-md-4">
                        <h5>Area</h5>
                        <div class="select-input disabled-first-option">
                            <input type="text" data-unit="Sq Ft" name="area" value="{{ $listing->username }}">
                        </div>
                        @if($errors->has('area'))
                        <span class="help-block text-danger">{{ $errors->first('area') }}</span>
                        @endif
                    </div>

                    <!-- Rooms -->
                    <div class="col-md-4">
                        <h5>Rooms</h5>
                        <select class="chosen-select-no-single" name="rooms">
                            <option label="blank"></option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                            <option>More than 5</option>
                        </select>
                        @if($errors->has('rooms'))
                        <span class="help-block text-danger">{{ $errors->first('rooms') }}</span>
                        @endif
                    </div>

                </div>
                <!-- Row / End -->

            </div>
            <!-- Section / End -->


            <!-- Section -->
            <h3>Gallery</h3>
            <div class="submit-section">
                <label for="title">Upload Images</label>
                <input type="file" name="images[]" multiple>
                @if($errors->has('images'))
                <span class="help-block text-danger">{{ $errors->first('images') }}</span>
                @endif
            </div>
            <!-- Section / End -->


            <!-- Section -->
            <h3>Location</h3>
            <div class="submit-section">

                <!-- Row -->
                <div class="row with-forms">

                    <!-- Address -->
                    <div class="col-md-6">
                        <h5>Address</h5>
                        <input type="text" name="address" value="{{ $listing->username }}">
                        @if($errors->has('address'))
                        <span class="help-block text-danger">{{ $errors->first('address') }}</span>
                        @endif
                    </div>

                    <!-- City -->
                    <div class="col-md-6">
                        <h5>City</h5>
                        <input type="text" name="city" value="{{ $listing->city }}">
                        @if($errors->has('city'))
                        <span class="help-block text-danger">{{ $errors->first('city') }}</span>
                        @endif
                    </div>

                    <!-- City -->
                    <div class="col-md-6">
                        <h5>State</h5>
                        <input type="text" name="state" value="{{ $listing->state }}">
                        @if($errors->has('state'))
                        <span class="help-block text-danger">{{ $errors->first('state') }}</span>
                        @endif
                    </div>

                    <!-- Zip-Code -->
                    <div class="col-md-6">
                        <h5>Zip-Code</h5>
                        <input type="text" name="zip_code" value="{{ $listing->zip_code }}">
                        @if($errors->has('zip_code'))
                        <span class="help-block text-danger">{{ $errors->first('zip_code') }}</span>
                        @endif
                    </div>

                </div>
                <!-- Row / End -->

            </div>
            <!-- Section / End -->


            <!-- Section -->
            <h3>Detailed Information</h3>
            <div class="submit-section">

                <!-- Description -->
                <div class="form">
                    <h5>Description</h5>
                    <textarea class="WYSIWYG" name="description" cols="40" rows="3" id="summary" spellcheck="true" value="{{ $listing->description }}"></textarea>
                    @if($errors->has('description'))
                    <span class="help-block text-danger">{{ $errors->first('description') }}</span>
                    @endif
                </div>

                <!-- Row -->
                <div class="row with-forms">

                    <!-- Balconies -->
                    <div class="col-md-4">
                        <h5>Balcony <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="balcony">
                            <option label="blank"></option>
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                        @if($errors->has('balcony'))
                        <span class="help-block text-danger">{{ $errors->first('balcony') }}</span>
                        @endif
                    </div>

                    <!-- Beds -->
                    <div class="col-md-4">
                        <h5>Bedrooms <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="bedrooms">
                            <option label="blank"></option>
                            <option value="-">None</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                        @if($errors->has('bedrooms'))
                        <span class="help-block text-danger">{{ $errors->first('bedrooms') }}</span>
                        @endif
                    </div>

                    <!-- Baths -->
                    <div class="col-md-4">
                        <h5>Bathrooms<span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="bathrooms">
                            <option label="blank"></option>
                            <option value="-">None</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                        @if($errors->has('bathrooms'))
                        <span class="help-block text-danger">{{ $errors->first('bathrooms') }}</span>
                        @endif
                    </div>



                    <!-- Kitchen -->
                    <div class="col-md-4">
                        <h5>Kitchen <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="kitchen">
                            <option label="blank"></option>
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                        @if($errors->has('kitchen'))
                        <span class="help-block text-danger">{{ $errors->first('kitchen') }}</span>
                        @endif
                    </div>

                    <div class="col-md-4">
                        <h5>Parking Space <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="parking_space">
                            <option label="blank"></option>
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                        @if($errors->has('parking_space'))
                        <span class="help-block text-danger">{{ $errors->first('parking_space') }}</span>
                        @endif
                    </div>
                    <!-- Baths -->
                    <div class="col-md-4">
                        <h5>Bathroom Type <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="bathroom_type[]">
                            @foreach($bathroom_types )
                            <option label="blank"></option>
                            <option>Ensuite</option>
                            <option>Shared</option>
                        </select>
                        @if($errors->has('bathroom_type'))
                        <span class="help-block text-danger">{{ $errors->first('bathroom_type') }}</span>
                        @endif
                    </div>

                </div>


                <!-- Row / End -->
                <!-- Row -->
                <div class="row with-forms">
                    <div class="col-md-4">
                        <h5>Toilet <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="toilet">
                            <option label="blank"></option>
                            <option value="-">None</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                        @if($errors->has('toilet'))
                        <span class="help-block text-danger">{{ $errors->first('toilet') }}</span>
                        @endif
                    </div>
                    <div class="col-md-4">
                        <h5>Water Supply <span>(optional)</span></h5>

                        <select class="chosen-select-no-single" name="water_supply">
                            <option label="blank"></option>
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                        @if($errors->has('water_supply'))
                        <span class="help-block text-danger">{{ $errors->first('water_supply') }}</span>
                        @endif
                    </div>
                    <div class="col-md-4">
                        <h5>Power Supply <span>(optional)</span></h5>
                        <select class="chosen-select-no-single" name="power_supply">
                            <option label="blank"></option>
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                        @if($errors->has('power_supply'))
                        <span class="help-block text-danger">{{ $errors->first('power_supply') }}</span>
                        @endif
                    </div>

                </div>
            </div>
            <!-- Section / End -->



            <div class="divider"></div>
            <button class="button preview margin-top-5">Edit Property <i class="fa fa-arrow-circle-right"></i></button>
            </div>
            <br>
        </form>
    </div>

</div>
</div>

@endsection

我尝试使用这种方法,但遇到了这个错误
SQLSTATE[23000]:完整性约束冲突:1048列“阳台”不能为空。我不希望要求用户填写所有内容,只需要添加新的数据。请问如何在选择框和文件输入上显示现有数据?。有更好的方法吗?

您可以将默认值传递给get请求。默认情况下,如果未填写该值,它将返回null。在您的情况下,您将需要一个空字符串:

$listing->balcony = $request->get('balcony', '');

您可以更改迁移以使字段为空

公共职能 { Schema::创建“清单”,函数蓝图$Blueprint{ //……其他领域 $blueprint->string'balyone'->可为空; }; }
将nullable添加到迁移中的阳台字段和任何其他不需要值的字段中。我在列字段中包含了nullable,并创建了一个新列表,但当我尝试编辑时遇到此错误ErrorException Undefined offset:1did您是否再次迁移数据库架构?是的,我确实迁移了:fresh,当我创建一个新的列表时,它可以正常工作,但是当我现在尝试转到编辑页面时,我得到了一个错误我不能说任何话,除非看到编辑页面的代码和控制器代码我现在在上面发布了它,当我尝试转到edit.blade.php文件时,我得到一个ErrorException未定义的偏移量:1请告诉我有关此项的更多信息。可能您试图在不检查$阳台的值是否为空的情况下关联它。你发布的错误说明不了什么。
<!-- Balconies -->
                <div class="col-md-4">
                    <h5>Balcony <span>(optional)</span></h5>
                    <select class="chosen-select-no-single" name="balcony">
                        <option label="blank"></option>
                        <option>Yes</option>
                        <option>No</option>
                    </select>
                    @if($errors->has('balcony'))
                    <span class="help-block text-danger">{{ $errors->first('balcony') }}</span>
                    @endif
                </div>
<!-- Balconies -->
                <div class="col-md-4">
                    <h5>Balcony <span>(optional)</span></h5>
                    <select class="chosen-select-no-single" name="balcony">
                        <option label="blank"></option>
                        <option value='yes' >Yes</option>
                        <option value='no'>No</option>
                    </select>
                    @if($errors->has('balcony'))
                    <span class="help-block text-danger">{{ $errors->first('balcony') }}</span>
                    @endif
                </div>
public function up()
{
    Schema::create('listings', function (Blueprint $blueprint) {

        $blueprint->string('balcony')->nullable();
    });
}