[英]QLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value laravel
I cannot store the company that my client belongs to.我无法存储我的客户所属的公司。 The company is a foreign key and when I click register customer I get an error:
公司是外键,当我点击注册客户时出现错误:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Tim' for column
customerlist
.SQLSTATE [22007]:日期时间格式无效:1366 integer 值不正确:
customerlist
列的“Tim”。customers
.customers
。company
at row 1 (SQL: insert intocustomers
(company
,name
,document
,phone
,updated_at
,created_at
) values (Tim, Felix Botta, 04919407939, +55.41984081085,felix.botta@gmail.com, 2021-02-14 20:55:15, 2021-02-14 20:55:15))第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))
CustomerController客户控制器
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.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');
}}
According to the migration the company
column is integer and foreign key that points to companies table and according to error you are passing string value Tim
for company
column.根据迁移,
company
列是 integer 和指向公司表的外键,根据错误,您正在为company
列传递字符串值Tim
。 So in blade view change所以在刀片视图中改变
<option value="{{ $company->name }}">{{ $company->name }}</option>
to至
<option value="{{ $company->id }}">{{ $company->name }}</option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.