Conflict of encoding using Tinker and Laravel

5

I'm learning how to use Laravel and got caught in an encoding problem. I am trying to use tinker to insert data into my tables, and there I am getting the following error when I try to include a new post:

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xA1tulo ...' for column 'title' at row 1 (SQL: insert into 'posts' ('title', 'body', 'updated_at', 'created_at') values (Título 1, Corpo do primeiro post, 2017-10-09 14:58:40, 2017-10-09 14:58:40))'

My migration responsible for the posts table looks like this:

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            // $table->charset = 'utf8';
            // $table->collation = 'utf8_general_ci';
            $table->increments('id');
            $table->string('title');
            $table->mediumText('body');
            $table->timestamps();
        });
    }

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

I'm using Windows 10, and to run the Laravel commands I'm using the cmder program. Here is an example post insertion I try to do:

$post = new App\post();
$post->title = 'Título 1';
$post->body = 'Corpo do primeiro post';
$post->save();

Could anyone tell me what I can do to insert accented words?

Edit: Here is a print of what appears in cmder:

    
asked by anonymous 09.10.2017 / 18:09

2 answers

0

This is complicated. I also have issues with accents using tinker . In the quick tests I make using tinker , I do not use any more accents.

In StackOverflow in English, I found the following topic: link

In it, user Van Niewelt says that the problem is CMD (command line), which does not handle accents very well. Although the topic commented on the use of accents in SQLite, it also applies to Postgres (BD that I use most) and, consequently, the others. I think it's true, because I've tried using Git Bash and Windows CMD, but both give the same problem with accentuation.

    
10.10.2017 / 03:19
0

The error:

  

SQLSTATE [HY000]: General error: 1366 Incorrect string value

Indicates that the table does not accept the character type, if this is actually your example:

$post = new App\post();
$post->title = 'Título 1';
$post->body = 'Corpo do primeiro post';
$post->save();

In this case the title did not come from the CMD and yet the error occurs because the script (Controller / Model) was not saved in the correct encoding, so open the script in an advanced text editor / processor like SublimeText or notepad ++ and save the document in the format UTF-8 without BOM , if everything is correct, then the problem was the moment you created the table the first time, so maybe you have to manually change it in the database, for example:

ALTER TABLE [NOME_DA_TABELA_AQUI] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general;

Now if the example you posted is unreal $post->title = 'Título 1'; and in fact the accent only fails when the data sees by Tinker, then it is probably another fault, if so, put the correct example.

    
10.10.2017 / 03:48