简体   繁体   English

QLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值 laravel

[英]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 into customers ( company , name , document , phone , email , 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:插入customerscompanynamedocumentphoneemailupdated_atcreated_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.

相关问题 Laravel 8:SQLSTATE[22007]:无效的日期时间格式:1366 不正确的 integer 值 - Laravel 8: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value SQLSTATE [22007]:无效的日期时间格式:1366不正确的整数值 - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value SQLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值:问题 - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: problem SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:Laravel 中的“column_name” - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'column_name' in Laravel SQLSTATE [22007]:无效的日期时间格式:1366错误的整数值:行1的列&#39;infraction_id&#39;的&#39;[“ 1”,“ 3”,“ 66”,“ 68”]&#39; - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[“1”,“3”,“66”,“68”]' for column 'infraction_id' at row 1 SQLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值:“isVisible”列的“true” - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'true' for column 'isVisible' 无效的日期时间格式:1366不正确的整数值: - Invalid datetime format: 1366 Incorrect integer value: 日期时间格式无效:1366 integer 值不正确 - Invalid datetime format: 1366 Incorrect integer value laravel SQLSTATE [22007]:无效的日期时间格式:1366不正确的十进制值:&#39;buy_amt * 0.00097812555575316&#39;为行1上的价格列错误 - laravel SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect decimal value: 'buy_amt * 0.00097812555575316' for column 'price' at row 1 error SQLSTATE[22007]:无效的日期时间格式:1366 字符串值不正确,html_entity_decode function - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value, html_entity_decode function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM