如何将表单中的数据发送到数据库中的表中?

发布于 2025-01-20 22:28:11 字数 5439 浏览 2 评论 0原文

我有一个表格,将新属性列表添加到数据库中的属性表中。我不断遇到错误,即输入是无效的,而Laravel也没有将输入的值抓住到SELECT HTML标签中。我将数据放在表格中,但它一直告诉我字段是无效的。

添加属性表格:

<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::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()方法:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2025-01-27 22:28:11

我可以在您的代码中看到错字

在刀片文件中,您使用了标签名称'props_basement',但在控制器中,您正在使用'prop_basement'

尝试以解决:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文