![](/img/trans.png)
[英]Laravel 8: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value
[英]QLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value laravel
我無法存儲我的客戶所屬的公司。 公司是外鍵,當我點擊注冊客戶時出現錯誤:
SQLSTATE [22007]:日期時間格式無效:1366 integer 值不正確:
customerlist
列的“Tim”。customers
。 第1行的company
(SQL:插入customers
(company
,name
,document
,phone
,updated_at
,created_at
)values(Tim,Felix Botta,04919407939,2019407939,2014214214214214214.GM19884085,FELIX1B.FELIX1B.FELIX1B.FELIXER,FELIX1B.FELIXER,FELIXB.FELIXER,FELIXER,FELIXER,FELIXER,FELIXER,FELIXER,FELIXER,FELIXER,FELIXER, 20:55:15, 2021-02-14 20:55:15))
客戶控制器
class CustomersController extends Controller
{
public function index(){
$customers = Customer::get();
return view('customers.list', ['customers' => $customers]);
}
public function new(){
$companies = Company::orderBy('id', 'desc')->get();
return view('customers.form', ['companies' => $companies]);
}
public function add( Request $request ){
$customers = new Customer;
$customers = $customers->create( $request->all() );
return Redirect::to('/customers');
}
public function edit( $id ){
$customers = Customer::findOrFail( $id );
return view('customers.form', ['customers' => $customers]);
}
public function update( $id, Request $request ){
$customers = Customer::findOrFail( $id );
$customers->update( $request->all() );
return Redirect::to('/customers');
}
public function delete( $id ){
$customers = Customer::findOrFail( $id );
$customers->delete();
return Redirect::to('/customers');
}}
form.blade
<form action="{{ url('customers/add') }}" method="post">
@csrf
<div class="form-group">
<label for="">Empresa:</label>
<select name="company" class="form-control">
@foreach($companies as $company)
<option value="{{ $company->name }}">{{ $company->name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Nome:</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">CPF / CNPJ:</label>
<input type="text" name="document" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Telefone:</label>
<input type="text" name="phone" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">E-mail:</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<button type="submit" class="btn btn-primary">Cadastrar</button>
</form>
@endif
</div>
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('name', 256);
$table->string('document', 256);
$table->string('phone', 128);
$table->string('email', 128);
$table->foreignId('company')->constrained('companies');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}}
根據遷移, company
列是 integer 和指向公司表的外鍵,根據錯誤,您正在為company
列傳遞字符串值Tim
。 所以在刀片視圖中改變
<option value="{{ $company->name }}">{{ $company->name }}</option>
至
<option value="{{ $company->id }}">{{ $company->name }}</option>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.