0

I have a form that adds a new property listing into the properties table in the db. I keep getting errors that inputs are null and also Laravel isn't grabbing the value inputted into the select HTML tag. I am putting data into the form, but it keeps telling me the fields are null.

Add property form:

<h1 class="admin-header">Add New Listing</h1>
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
<form method="POST" action="{{ route('admin.store_properties') }}" id="add_property_form" enctype="multipart/form-data">
    @csrf
    <div>
        <label for="prop_title">Property Title</label>
        <input type="text" id="prop_title" />
    </div>

    <div>
        <label for="prop_desciption">Property Description</label>
        <textarea name="prop_desciption" id="prop_desciption"></textarea>
    </div>

    <div>
        <label for="prop_img">Property Image</label>
        <input type="file" name="prop_img" id="prop_img" required />
    </div>

    <div>
        <label for="prop_beds">Number of Bedrooms</label>
        <input type="number" name="prop_beds" id="prop_beds" steps="1" min="1" />
    </div>

    <div>
        <label for="prop_baths">Number of Bathrooms</label>
        <input type="number" name="prop_baths" id="prop_baths" />
    </div>

    <div>
        <label for="prop_ft">Sqaure Feet</label>
        <input type="number" name="prop_ft" id="prop_ft" />
    </div>

    <div>
        <label for="props_basement">Finished Basement?</label>
        <select name="props_basement" id="props_basement">
            <option value="" selected disabled>Select an option</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
        </select>
    </div>

    <div>
        <label for="prop_tax">Property Tax</label>
        <input type="number" name="prop_tax" id="prop_tax" />
    </div>

    <div>
        <label for="props_heat">Heat Type</label>
        <select name="props_heat" id="props_heat">
            <option value="" selected disabled>Select an option</option>
            <option value="gas">Gas</option>
            <option value="oil">Oil</option>
            <option value="electric">Electric</option>
        </select>
    </div>

    <div>
        <label for="props_waterheater">Finished Basement?</label>
        <select name="props_waterheater" id="props_waterheater">
            <option value="" selected disabled>Select an option</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
        </select>
    </div>

    <div>
        <label for="prop_year">Year Built</label>
        <input type="number" name="prop_year" id="prop_year" />
    </div>

    <button type="submit">Add New Listing</button>

</form>

Route:

Route::group(['prefix' => 'admin'], function() {

    Route::get('/', function() {
        return view('admin.dashboard');
    })->middleware('auth');

    Route::get('/properties', [PropertiesController::class, 'index'])->middleware('auth');
    Route::get('/properties/create', [PropertiesController::class, 'create'])->middleware('auth');
    Route::post('/properties/store-post', [PropertiesController::class, 'store'])->name('admin.store_properties')->middleware('auth');

});

Controller store() method:

public function store(Request $request)
    {

        // Create a new Property and store it in the properties DB
        $prop = new Property;
        $path;

        if ($request->hasFile('prop_img')) {
           
            // Get filename with extension
             $filenameWithExt = $request->file('prop_img')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just extension
            $extension = $request->file('prop_img')->getClientOriginalExtension();
 
            // Filename to store
            $filenameToStore = $filename . '_' . time() . '.' . $extension;
 
            // Upload Image
            $path = $request->file('prop_img')->storeAs('public/property_images', $filenameToStore);
         } else {
             // Filename to store
            $filenameToStore = 'noimage.jpg';
         }

        $prop->property_title = $request->input('prop_title');
        $prop->property_description = $request->input('prop_desc');
        $prop->property_image = $path;
        $prop->bedrooms = $request->input('prop_beds');
        $prop->bathrooms = $request->input('prop_baths');
        $prop->square_feet = $request->input('prop_ft');
        $prop->finished_basement = $request->input('prop_basement');
        $prop->prop_tax = $request->input('prop_tax');
        $prop->heat_type = $request->input('prop_heat');
        $prop->water_heater = $request->input('prop_waterheater');
        $prop->year_built = $request->input('prop_year');
        $prop->save();
        return view('admin.add_property');
    }
4
  • Well you do have some volatile variable like $filenameToStore present in the else and is not used. and $path that is null if no image is uploaded. Maybe add to your question the exact error you are getting and the results of dd($request->all()) Commented Apr 12, 2022 at 16:40
  • I am getting this error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'property_title' cannot be null but I am filling the title field. Commented Apr 12, 2022 at 16:45
  • All your table columns can be filled in form? Commented Apr 12, 2022 at 18:43
  • 1
    @webdev1995 you are not filling the field. <input type="text" id="prop_title" /> has no name attribute. Commented Apr 12, 2022 at 19:05

1 Answer 1

0

I can see a typo in your code

In blade file you used tag name 'props_basement' but in controller you are using 'prop_basement'

Try this to fix:

<select name="prop_basement" id="props_basement">
        <option value="" selected disabled>Select an option</option>
        <option value="yes">Yes</option>
        <option value="no">No</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.