Home > database >  How can I send data from a form into the table in the database?
How can I send data from a form into the table in the database?

Time:04-14

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 >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');
    }

CodePudding user response:

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>
  • Related