Coding conflict using Tinker and Laravel

Asked

Viewed 531 times

7

I’m learning how to use Laravel and grabbed on to an encoding problem. I’m trying to use the Tinker to insert data into my tables, and there I’m 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 table posts is 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 am using Windows 10, and to run the commands of Laravel I am using the program Cmder. Here’s an example of post insertion I try to do:

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

Would someone tell me what I can do to insert accented words to work?

Edit: Follow a print of what appears on Cmder: Erro no cmder

2 answers

1

This one is complicated. I also have problems with accents using the tinker. In the quick tests I do using the tinker, I no longer use accents.

At Stackoverflow in English, I found the following topic:: https://stackoverflow.com/questions/34458520/laravel-eloquent-not-receiving-accented-characters-from-sqlite-database

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

  • But his example does not go through the CMD, of course you can be correct, but only if the example of the author of the question is not real and he did not pass the correct details.

  • Well observed. I imagined that what he posted after Migration was a sequence of commands from tinker. As I have been through this, I imagined it would be something like this. But let’s wait for the author clarify.

  • The problem only happens in CMD, via personal Tinker. When I run the same code, directly on the pages, everything is ok. It’s funny that on another machine, I asked a friend to take the same test, and it worked on his computer. It’s like my environment is producing this coding conflict. I came to create a project from scratch and repeated the test, and nothing, all in the same. Could be some configuration of my wamp, my operating system, or something external of that kind interfering?

1

The mistake:

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

and in this case the title did not come from 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 such as Sublimetext or Notepad++ and save the document in the format UTF-8 NO GOOD, if everything is correct, then the problem was at the time you first created the table, so you may have to change it manually in the bank, 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 actually the accent only fails when the data is seen by Tinker, so it’s probably another failure, if that’s the case post the correct example.

  • Guilherme I think the problem is beyond the coding of the editor and the bank. My database and my tables are in utf8_unicode_ci. My editor also has the utf8 encoding. When I execute directly in the code what I put in Tinker, the Insert works and goes with accent normally for the bank. Everything ok. I only have the problem in CMD by Tinker. I tried to use another application besides CMDER and still the problem. The chcp of the page is 850, when I change to 65001 the Insert works but in place of the accent or any special character is the symbol 0. Any idea?

  • @churros then the such example you posted in the question is unreal and does not match, ie always make examples as the failure. After the print of your image I think it’s now clear, I’ll run the tests here and download Cmder and let you know.

  • Thank you for your attention William, any news, we are there. Just one last detail, I don’t know if you saw my other answer, but I tried the same procedure on another machine at the office where I work and I didn’t have this coding problem. Maybe you don’t either.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.