B
B
BonBon Slick2017-02-01 09:34:44
PostgreSQL
BonBon Slick, 2017-02-01 09:34:44

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~ unknown LINE 1?

It worked in MySQL, now on PostgreSQL when trying to search:

public function search(Request $request, $counter = 15)
  {
    $search_data = $request->input('searcher');
    if ($request->has('searcher')){
     $results = Product::where('name', 'LIKE', '%'. $search_data .'%')
      $results = Product::where('name', 'LIKE', '%'. strval($search_data) .'%')
     ->orWhere('product_id', 'LIKE', '%'. intval($search_data) .'%')
     ->orWhere('bar_code', 'LIKE', '%'. intval($search_data) .'%')
     ->orWhere('brand', 'LIKE', '%'. strval($search_data) .'%')
     ->orderBy('product_id', 'asc')
     ->paginate( $counter );
     return view('products/search', compact('results') );
   }
 }

Mistake:
QueryException in Connection.php line 770:
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~ unknown
LINE 1: ...m "products" where "name" LIKE $1 or "product_id" LIKE $2 or...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select count(*) as aggregate from "products" where "name" LIKE %exte% or "product_id" LIKE %exte% or "bar_code" LIKE %exte% or "brand" LIKE %exte%)
in Connection.php line 770
at Connection->runQueryCallback('select count(*) as aggregate from "products" where "name" LIKE ? or "product_id" LIKE ? or "bar_code" LIKE ? or "brand" LIKE ?', array('%exte%', '%exte%', '%exte%', '%exte%'), object(Closure)) in Connection.php line 726
at Connection->run('select count(*) as aggregate from "products" where "name" LIKE ? or "product_id" LIKE ? or "bar_code" LIKE ? or "brand" LIKE ?', array('%exte%', '%exte%', '%exte%', '%exte%'), object(Closure)) in Connection.php line 351
....

It cannot search if the search types are different, I tried:
cast($search_data as integer)
//или так
 ->orWhere('cast(bar_code AS integer)', 'LIKE', '%'. $search_data .'%')
// и в классе 
 protected $cast = [
    'bar_code' => 'integer',
    ];

In DB:
bar_code	integer	[uniq]	Browse	Alter	Privileges	Drop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2017-02-01
@BonBonSlick

EUREKA!
After 4 hours of googling, smoking man PostgreSQL 9.6 and Laravel 5.4 found a solution:

DB::table('products')->whereRaw("CAST(product_id AS TEXT) ILIKE '%11%'")->first();
// или
 $results = Product::where('name', 'LIKE', '%'. $search_data .'%')
     ->orWhereRaw("CAST(product_id AS TEXT) ILIKE '%$search_data%'")

It is important that, in the Product class, this code does not work in this case, as it is written in the Laravel mana:
protected $casts = [
    'product_id' => 'string',
    ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question