Home > Software engineering >  I am trying to insert data into a table but it gives me "Column not found"
I am trying to insert data into a table but it gives me "Column not found"

Time:08-12

The error is:

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count_service_email' in 'field list' (SQL: insert into countService (count_service_email, password, n_remaining_customers, in_day, service_id, updated_at, created_at) values ([email protected], netflix123, 10, true, 1, 2022-08-11 12:19:01, 2022-08-11 12:19:01))"

How can I solve this?

My model:

<?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;

    class CountService extends Model
   {
    use HasFactory;

    protected $table = 'countService';

    protected $fillable = [
        'count_service_email',
        'password',
        'n_remaining_customers',
        'in_day',
        'sold_off',
        'service_id'
    ];
  }

My migrations:

<?php

    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;

    class CreateCountServiceTable extends Migration
   {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('countService')) {
            Schema::create('countService', function (Blueprint $table) {
                $table->id();
                $table->string('count_service_email')->unique();
                $table->string('password');
                $table->smallInteger('n_remaining_customers');
                $table->boolean('in_day');
                $table->boolean('solt_off');
                $table->smallInteger('service_id')->unsigned();
                $table->foreign('service_id')->references('id')->on('services');
                $table->timestamps();
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('count');
    }
   }

My controller:

    public function store(Request $request)
    {
        $input = $request->all();

        $validator = Validator::make($input, [
            'count_service_email' => 'required',
            'password' => 'required',
            'n_remaining_customers' => 'required',
            'in_day' => 'required',
            'solt_off' => 'required',
            'service_id' => 'required'
        ]);

        if ($validator->fails()) {
            return $this->sendError('Validation of store count service error.', $validator- 
       >errors());
        }
    
        $count = CountService::create($input);
        return $this->sendResponse(new CountServiceResource($count), 'Count of service 
       create with successfull');
       }

CodePudding user response:

By the error, the it clearly shows that your table doesn't have column count_service_email in your table.

CodePudding user response:

Thanks to all who replied! with your help I solved the problem, the steps to solve the problem was: first -> eliminating the : if (!Schema::hasTable('countService')) second -> change the service_id data type from smallInteger to bigInteger third -> run the php artisan migrate:fresh and fortunately it worked

  • Related