SQLSTATE[23000]: 完整性约束违规: 1048 列 ‘county_id’ 不能为空

huangapple go评论58阅读模式
英文:

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 表上的 idsc_id 引用 subcounty 表上的 idtown_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&#39;m having errors doing so.

This is the error I am getting.


`` SQLSTATE[23000]: Integrity constraint violation: 1048 Column &#39;county_id&#39; 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>

        &lt;p&gt;Plot No&lt;/p&gt;&lt;input type=&quot;number&quot;   required name=&quot;plot_no&quot; value=&quot;{{old(&#39;plot_no&#39;)}}&quot;  class=&quot;form-control @error(&#39;plot_no&#39;) is-invalid @enderror&quot;
placeholder=&quot;Please enter plot number&quot;&gt; 
&lt;div class=&quot;invalid-feedback&quot;&gt;
@error(&#39;plot_no&#39;) {{$message}} @enderror
&lt;/div&gt;
&lt;p&gt;Property Description&lt;/p&gt;&lt;input type=&quot;text&quot;   required name=&quot;Property_Ad&quot; value=&quot;{{old(&#39;Property_Ad&#39;)}}&quot;  class=&quot;form-control @error(&#39;Property_Ad&#39;) is-invalid @enderror&quot;
placeholder=&quot;Please enter property description&quot;&gt;
&lt;div class=&quot;invalid-feedback&quot;&gt;
@error(&#39;Property_Ad&#39;) {{$message}} @enderror
&lt;/div&gt;
&lt;p&gt;Property Features&lt;/p&gt;&lt;input type=&quot;text&quot;   required name=&quot;Property features&quot; value=&quot;{{old(&#39;Property_features&#39;)}}&quot;  class=&quot;form-control @error(&#39;Property_features&#39;) is-invalid @enderror&quot;
placeholder=&quot;Please enter property features&quot;&gt;
&lt;div class=&quot;invalid-feedback&quot;&gt;
@error(&#39;Property_features&#39;) {{$message}} @enderror
&lt;/div&gt;
&lt;p&gt;Property Images&lt;/p&gt;&lt;input type=&quot;file&quot; required name=&quot;Property_Photo&quot; value=&quot;{{old(&#39;Property_Photo&#39;)}}&quot;  class=&quot;form-control&quot;
&gt;
&lt;p&gt;Location&lt;/p&gt;
&lt;div class=&quot;row align-items-start&quot;&gt;

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

         &lt;p&gt;Property Type&lt;/p&gt;&lt;input type=&quot;text&quot;   required name=&quot;property_type&quot; value=&quot;{{old(&#39;type&#39;)}}&quot; title=&quot;2-10 characters&quot;  class=&quot;form-control @error(&#39;type&#39;) is-invalid @enderror&quot;
placeholder=&quot;Please enter your property type&quot;&gt;
&lt;div class=&quot;invalid-feedback&quot;&gt;
@error(&#39;type&#39;) {{$message}} @enderror
&lt;/div&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;form-control btn btn-success&quot; &gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

</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(&#39;HO.addproperty&#39;);
}
public function saveProperty(Request $request){
$prop = new property();
$prop-&gt;National_id=$request-&gt;National_id;
$prop-&gt;Plot_No=$request-&gt;plot_no;
$prop-&gt;Property_Ad=$request-&gt;Property_Ad;
$prop-&gt;Property_Photo=$request-&gt;Property_Photo;
$prop-&gt;Property_features=$request-&gt;Property_features;
$prop-&gt;county_id=$request-&gt;county_id;
$prop-&gt;sc_id=$request-&gt;sc_id;
$prop-&gt;town_id=$request-&gt;town_id;
$prop-&gt;type=$request-&gt;type;
if($request-&gt;hasFile(&#39;Property_photo&#39;)){
$file = $request-&gt;file(&#39;Property_photo&#39;);
$extension = $file-&gt;getClientOriginalExtension();
$filename = time().&#39;.&#39;.$extension;
$file-&gt;move(&#39;uploads/properties&#39;, $filename);
$prop-&gt;Property_Photo = $filename;
}
$prop-&gt;save();
return redirect(&#39;properties&#39;)-&gt;with(&#39;status&#39;,&quot;New Property added.&quot;);
}
public function editProperty(){
}

}


</details>
# 答案1
**得分**: 0
基本上,它指示 `county_id` 字段被发送为 `空` 或 `null` 到数据库中。在你的表单中,`county_id` 字段被称为 `County`,你需要将它更改为:
`&lt;select id=&quot;County&quot; class=&quot;form-select-lg mb-3 form-control&quot; wire:model = &quot;selectedCounty&quot; name=&quot;county_id&quot;&gt;`
<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:
`&lt;select id=&quot;County&quot; class=&quot;form-select-lg mb-3 form-control&quot; wire:model = &quot;selectedCounty&quot; name=&quot;County&quot;&gt;`
to 
`&lt;select id=&quot;County&quot; class=&quot;form-select-lg mb-3 form-control&quot; wire:model = &quot;selectedCounty&quot; name=&quot;county_id&quot;&gt;`
</details>

huangapple
  • 本文由 发表于 2023年2月18日 18:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定