简体   繁体   中英

ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause'

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 categories where ( 0 = active and 1 = is active) order by name asc)

-> 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'])

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();

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