如何将表单中的数据发送到数据库中的 table?
How can I send data from a form into the table in the database?
我有一个表单可以将新的 属性 列表添加到数据库的属性 table 中。我不断收到输入为空的错误,而且 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');
});
控制器存储()方法:
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');
}
我发现你的代码中有错别字
In blade file you used tag name 'props_basement' but in controller you are using '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>
我有一个表单可以将新的 属性 列表添加到数据库的属性 table 中。我不断收到输入为空的错误,而且 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');
});
控制器存储()方法:
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');
}
我发现你的代码中有错别字
In blade file you used tag name 'props_basement' but in controller you are using '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>