![](/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.