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?
<!-- 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();
});
}
You can change your migration to make the fields nullable.
public function up()
{
Schema::create('listings', function (Blueprint $blueprint) {
// ... other fields
$blueprint->string('balcony')->nullable();
});
}
You can pass in a default value to theget()
request. By default, it will return null if the value is not filled out. In your case, you'll want an empty string:
$listing->balcony = $request->get('balcony', '');
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.