I’m trying to edit existing property’s listings in my laravel application using the following method
ListingController
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!'); }
the 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
I tried using this method but was confronted with this error SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘balcony’ cannot be null. I don’t want the user to be required to fill everything just new data that is needed to be added. Please how can I displayed existing data on the select box and file input?. is there a better way to do this?
Advertisement
Answer
<!-- 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>
as you can see in the balcony section code, you don’t have value attribute on your options element so it should be like that
<!-- 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>
Besides that you can change the default value to Nullable of the column fields which are not required fields through phpmyadmin or through migrations and refresh the migrations again like:
public function up() { Schema::create('listings', function (Blueprint $blueprint) { $blueprint->string('balcony')->nullable(); }); }