简体   繁体   中英

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

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 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. So in blade view change

<option value="{{ $company->name }}">{{ $company->name }}</option>

to

<option value="{{ $company->id }}">{{ $company->name }}</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM