I have a Laravel-Page and since i want to add a “soft delete” with the active field in the DB i get that error:
emails.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘0’ in ‘where clause’ (SQL: select * from
categorieswhere (0= active and1= is active) order bynameasc)
-> which causes a error 500 obviously
and I have no plan how to fix this cause it looks like everything is fine.
DB-Layout
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image')->nullable();
$table->string('description')->nullable();
$table->unsignedBigInteger('userID')->nullable();
$table->string('active', 20)->default('is active');
$table->timestamps();
});
Model:
protected $table = 'categories'; protected $fillable = ['id', 'name', 'image', 'description', 'userID', 'active', 'created_at', 'updated_at'];
Usage(as sample for all other):
$categories = DB::table('categories')
->where(['active', 'is active'])
->orderBy('name', 'asc')
->get();
or something like this:
->where(['categoryID', $category->id], ['active', 'is active'])
Advertisement
Answer
This code is causing error.
$categories = DB::table('categories')
->where(['active', 'is active'])
->orderBy('name', 'asc')
->get();
Eloquent is expecting your where array to be in key and value pair. As you have not given it, it is using index as column name.
You should place it like this:
$categories = DB::table('categories')
->where('active', 'is active') //will also work ->where('active', '=', 'is active')
->orderBy('name', 'asc')
->get();