英文:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'county_id' cannot be null
问题
我正在尝试将动态选择的下拉菜单中的数据保存到我的数据库,但在这样做时出现了错误。
这是我得到的错误信息。
SQLSTATE[23000]: 完整性约束违规: 1048 列 'county_id' 不能为 null (SQL: insert into `properties` (`National_id`, `Plot_No`, `Property_Ad`, `Property_Photo`, `Property_features`, `county_id`, `sc_id`, `town_id`, `type`, `updated_at`, `created_at`) values (63636, 424545, Duplex Apartments, test.jpg, Free parking, ?, ?, ?, ?, 2023-02-18 09:42:30, 2023-02-18 09:42:30))
我需要帮助,以将选定的下拉菜单保存到一个数据库中,该数据库是引用另一个表的外键(county_id
引用 county
表上的 id
,sc_id
引用 subcounty
表上的 id
,town_id
引用 town
表上的 id
)。
这是我的 Livewire 页面。
<form id="add-property-form" method="POST" action="{{URL::to('saveProperty')}}">
@csrf
<div class="col-md-8">
<div class="card">
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li class="text-white">{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<!-- 输入模式="[a-zA-Z]{2,10}$" 必需 -->
<p>National id</p>
<input type="number" required name="National_id" value="{{old('National_id')}}" class="form-control @error('National_id') is-invalid @enderror" placeholder="确认您的国民身份证号码">
<div class="invalid-feedback">
@error('National_id') {{$message}} @enderror
</div>
<p>Plot No</p>
<input type="number" required name="plot_no" value="{{old('plot_no')}}" class="form-control @error('plot_no') is-invalid @enderror" placeholder="请输入地块号码">
<div class="invalid-feedback">
@error('plot_no') {{$message}} @enderror
</div>
<p>Property Description</p>
<input type="text" required name="Property_Ad" value="{{old('Property_Ad')}}" class="form-control @error('Property_Ad') is-invalid @enderror" placeholder="请输入物业描述">
<div class="invalid-feedback">
@error('Property_Ad') {{$message}} @enderror
</div>
<p>Property Features</p>
<input type="text" required name="Property features" value="{{old('Property_features')}}" class="form-control @error('Property_features') is-invalid @enderror" placeholder="请输入物业特点">
<div class="invalid-feedback">
@error('Property_features') {{$message}} @enderror
</div>
<p>Property Images</p>
<input type="file" required name="Property_Photo" value="{{old('Property_Photo')}}" class="form-control">
<p>Location</p>
<div class="row align-items-start">
<div class="col">
<select id="County" class="form-select-lg mb-3 form-control" wire-model="selectedCounty" name="County">
<option value="">County</option>
@foreach ($counties as $co)
<option value="{{$co->id}}">{{$co->name}}</option>
@endforeach
</select>
</div>
@if (!is_null($subcounty))
<div class="col">
<select id="subCounty" class="form-select-lg mb-3 form-control" wire-model="selectedSubCounty" name="subCounty">
<option value="">SubCounty</option>
@foreach ($subcounty as $su)
<option value="{{$su->id}}">{{$su->name}}</option>
@endforeach
</select>
</div>
@endif
@if (!is_null($town))
<div class="col">
<select id="Town" class="form-select-lg mb-3 form-control" wire-model="selectedTown" name="Town">
<option value="">Town</option>
@foreach ($town as $to)
<option value="{{$to->id}}">{{$to->name}}</option>
@endforeach
</select>
</div>
@endif
</div>
<p>Property Type</p>
<input type="text" required name="property_type" value="{{old('type')}}" title="2-10个字符" class="form-control @error('type') is-invalid @enderror" placeholder="请输入您的物业类型">
<div class="invalid-feedback">
@error('type') {{$message}} @enderror
</div>
</div>
<input type="submit" name="submit" class="form-control btn btn-success">
</div>
</div>
</div>
</form>
这是我的视图页面
@extends('layout')
@section('headTitle', 'Your Properties / Add Property')
@section('pageTitle', 'Your Properties / Add Property')
@section('content')
<livewire:subcounty />
@endsection
这是我的控制器页面。
<?php
namespace App\Http\Controllers;
use App\Models\home_owners;
use App\Models\property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ProController extends Controller
{
public function viewProperties(){
$allPros = property::paginate(7);
//dd is the laravel version of var_dump() & die();
//dd($allRoles);
return view('HO.properties', ['properties'=>$allPros]);
}
public function addProperty(){
return view('HO.addproperty');
}
public function saveProperty(Request $request){
$prop = new property();
$prop->National_id=$request->National_id;
$prop->Plot_No=$request->plot_no;
$prop->Property_Ad=$request->Property_Ad;
$prop->Property_Photo=$request->Property_Photo;
$prop->Property_features=$request->Property_features;
$prop->county_id=$request->county_id;
$prop->sc_id=$request->sc_id;
$prop->town_id=$request->town_id;
$prop->type=$request->type;
if($request->hasFile('Property_photo')){
$file = $request->file('Property_photo');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/
<details>
<summary>英文:</summary>
I am trying to save my data from a dynamic selected dropdown into my database but I'm having errors doing so.
This is the error I am getting.
`` SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'county_id' cannot be null (SQL: insert into `properties` (`National_id`, `Plot_No`, `Property_Ad`, `Property_Photo`, `Property_features`, `county_id`, `sc_id`, `town_id`, `type`, `updated_at`, `created_at`) values (63636, 424545, Duplex Apartments, test.jpg, Free parking, ?, ?, ?, ?, 2023-02-18 09:42:30, 2023-02-18 09:42:30)) ``
I need help on how to save a selected drop down to a database which is a foreign key referencing another table (county_id references id on the county table, sc_id references id on the subcounty table and town_id references id on the town table).
This is my livewire page.
<form id="add-property-form" method="POST" action="{{URL::to('saveProperty')}}">
@csrf
<div class="col-md-8">
<div class="card">
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li class="text-white">{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class= "form-group">
<!--input pattern= "[a-zA-Z]{2,10}$" required-->
<p>National id</p><input type="number" required name="National_id" value="{{old('National_id')}}" class="form-control @error('National_id') is-invalid @enderror"
placeholder="Confirm your National id number">
<div class="invalid-feedback">
@error('National_id') {{$message}} @enderror
</div>
<p>Plot No</p><input type="number" required name="plot_no" value="{{old('plot_no')}}" class="form-control @error('plot_no') is-invalid @enderror"
placeholder="Please enter plot number">
<div class="invalid-feedback">
@error('plot_no') {{$message}} @enderror
</div>
<p>Property Description</p><input type="text" required name="Property_Ad" value="{{old('Property_Ad')}}" class="form-control @error('Property_Ad') is-invalid @enderror"
placeholder="Please enter property description">
<div class="invalid-feedback">
@error('Property_Ad') {{$message}} @enderror
</div>
<p>Property Features</p><input type="text" required name="Property features" value="{{old('Property_features')}}" class="form-control @error('Property_features') is-invalid @enderror"
placeholder="Please enter property features">
<div class="invalid-feedback">
@error('Property_features') {{$message}} @enderror
</div>
<p>Property Images</p><input type="file" required name="Property_Photo" value="{{old('Property_Photo')}}" class="form-control"
>
<p>Location</p>
<div class="row align-items-start">
<div class="col">
<select id="County" class="form-select-lg mb-3 form-control" wire:model = "selectedCounty" name="County">
<option value="" >County</option>
@foreach ($counties as $co)
<option value="{{$co->id}}">{{$co->name}}</option>
@endforeach
</select>
</div>
@if (!is_null($subcounty))
<div class="col">
<select id="subCounty" class="form-select-lg mb-3 form-control" wire:model = "selectedSubCounty" name="subCounty">
<option value="">SubCounty</option>
@foreach ($subcounty as $su)
<option value="{{$su->id}}">{{$su->name}}</option>
@endforeach
</select>
</div>
@endif
@if (!is_null($town))
<div class="col">
<select id="Town" class="form-select-lg mb-3 form-control" wire:model = "selectedTown" name="Town">
<option value="">Town</option>
@foreach ($town as $to)
<option value="{{$to->id}}">{{$to->name}}</option>
@endforeach
</select>
</div>
@endif
</div>
<p>Property Type</p><input type="text" required name="property_type" value="{{old('type')}}" title="2-10 characters" class="form-control @error('type') is-invalid @enderror"
placeholder="Please enter your property type">
<div class="invalid-feedback">
@error('type') {{$message}} @enderror
</div>
</div>
<input type="submit" name="submit" class="form-control btn btn-success" >
</div>
</div>
</div>
</div>
</form>
This is my view page
@extends('layout')
@section('headTitle', 'Your Properties / Add Property')
@section('pageTitle', 'Your Properties / Add Property')
@section('content')
<livewire:subcounty />
@endsection
This is my controller page.
<?php
namespace App\Http\Controllers;
use App\Models\home_owners;
use App\Models\property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ProController extends Controller
{
public function viewProperties(){
$allPros = property::paginate(7);
//dd is the laravel version of var_dump() & die();
//dd($allRoles);
return view('HO.properties', ['properties'=>$allPros]);
}
public function addProperty(){
return view('HO.addproperty');
}
public function saveProperty(Request $request){
$prop = new property();
$prop->National_id=$request->National_id;
$prop->Plot_No=$request->plot_no;
$prop->Property_Ad=$request->Property_Ad;
$prop->Property_Photo=$request->Property_Photo;
$prop->Property_features=$request->Property_features;
$prop->county_id=$request->county_id;
$prop->sc_id=$request->sc_id;
$prop->town_id=$request->town_id;
$prop->type=$request->type;
if($request->hasFile('Property_photo')){
$file = $request->file('Property_photo');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/properties', $filename);
$prop->Property_Photo = $filename;
}
$prop->save();
return redirect('properties')->with('status',"New Property added.");
}
public function editProperty(){
}
}
</details>
# 答案1
**得分**: 0
基本上,它指示 `county_id` 字段被发送为 `空` 或 `null` 到数据库中。在你的表单中,`county_id` 字段被称为 `County`,你需要将它更改为:
`<select id="County" class="form-select-lg mb-3 form-control" wire:model = "selectedCounty" name="county_id">`
<details>
<summary>英文:</summary>
basically it is indicating that the `county_id` field is being sent `empty` or `null` to the database, in your form the `county_id` field is called `County`, you need to change it:
`<select id="County" class="form-select-lg mb-3 form-control" wire:model = "selectedCounty" name="County">`
to
`<select id="County" class="form-select-lg mb-3 form-control" wire:model = "selectedCounty" name="county_id">`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论