jasonbischak's avatar

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

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.

Form: `

Add New Listing

@if($errors->any())

{{$errors->first()}}

@endif @csrf Property Title
<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>
`

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

`

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

}); `

0 likes
4 replies
siangboon's avatar

it's hard to tell what's going wrong without the actual error message...

but, usually the typo error and also the fillable field may be the most common mistakes...

btw, it may be good practice to validate the input before doing anything... it help you to identify the errors easier too...

1 like
jasonbischak's avatar

@siangboon The current error I am seeing it that property_title cannot be null, but I am filling the title input field.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'property_title' cannot be null

jlrdw's avatar

@jasonbischak seems you are missing some inputs.

$prop->property_title = $request->input('prop_title');  // From where?
1 like
siangboon's avatar

@jasonbischak check the spelling, dd the $request->input('prop_title') and see what you get... and double check the property_title is in the $fillable field list...

Please or to participate in this conversation.