I am using Laravel 5.3 and the latest version of the dataTables plugin. Before working with postgres, migrate to mysql for needs of the db, then I encounter a problem, the custom filtering throws me an error that is as follows:
SQLSTATE [HY000]: General error: 1267 Illegal mix of collations (utf8_bin, NONE) and (binary, COERCIBLE) for operation 'like' (SQL: select count (*) as aggregate from (select '1' as
row_count
fromusers
whereusers
.id
! = 1 and CONCAT (users.name, '', users.cedula, '', users.nombre, '', users.last) like% pa%) count_row_table)
Note that you do not create the signs ('') around the like
parameter.
Then to verify that it is not the collation
that you put in the name
column to sensitize capital letters to a fidder, here the fiddler , you can see what I tell you if you just put the ('') mysql throws the expected result, different case throws an error.
I add the backend code of my app where I consult the table:
public function tabla(Request $request){
try{
$sqls = User::select(array(
'users.id',
'users.name',
'users.cedula',
'users.nombres',
'users.apellidos',
'users.tipo',
'users.status',
))
->where('users.id', '!=', Auth::user()->id);
return Datatables::of($sqls)
->filter(function ($query) use ($request) {
if ($request->has('campo')) {
$campo = $request->get('campo');
$query->where(DB::raw("CONCAT( users.name, ' ', users.cedula , ' ', users.nombres , ' ', users.apellidos )"), 'like', "%{$campo}%");
}
if ($request->has('tipo')) {
$query->where('users.tipo', '=', "{$request->get('tipo')}");
}
if ($request->has('estados')) {
$query->where('users.status', '=', "{$request->get('estados')}");
}
})
->make();
}catch(\Illuminate\Database\QueryException $e){
$array = array(
'mensaje' => $e->getMessage(),
'codigo' => $e->getCode(),
'sql' => $e->getSql(),
'bindings' => $e->getBindings(),
);
return Response::json(array('ErrorSql' => $array));
}
}
I look for the error in mysql
and get this , but I can not understand why I get that error.
Update 1:
Having determined that the problem of like
is for the collation
of the table and database, I only have one problem, how to adapt it to a migration?.
Create the migration as follows:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTablesAndDatabase extends Migration
{
public function up()
{
$sql = 'ALTER DATABASE instivoc_03 CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE users CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE materiales CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE evaluaciones CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE preguntas CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE preguntas_selecciones CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
$sql = 'ALTER TABLE respuestas CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ';
DB::connection()->getPdo()->exec($sql);
}
public function down()
{
}
}
How do I determine the name of the db
to which it connects? Let me explain: if the connection were to be manipulated by someone X and changed to another name of another db
the migration would have to be edited too, how could I avoid that?