Я хотел бы использовать DB-класс laravel для выполнения запроса mysql, но ни одна из функций, предоставляемых Laravel, не работает.
Ни один из них не работает: DB :: statment () / DB :: select () / DB :: raw () / DB :: update () / DB :: select (DB :: raw ())
Вот код, который я хотел бы запросить:
DROP TABLE users; CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `u_birthday` date NOT NULL DEFAULT '0000-00-00', `u_lastlogin` int(11) NOT NULL, `u_logcout` int(11) NOT NULL DEFAULT '0', `u_level` tinyint(1) NOT NULL DEFAULT '0', `u_language` tinyint(1) NOT NULL DEFAULT '0', `u_status` tinyint(1) NOT NULL DEFAULT '0', `u_gender` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO users VALUES("1","admin","admin@example.com","$2y$08$9sBJh7iyF9Yr6xvsieNmBOsotGPBkZFydVjb/Yk5Fzh4IGBVo7Je6","0000-00-00 00:00:00","0000-00-00","0","0","9","0","0","0"); INSERT INTO users VALUES("2","moderator","mod@example.com","$2y$08$15/tIKPM/8gATSzkmey5tuAA/PL4lJeFq7liTETyz0h1dkOotwp3G","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("3","helper","help@example.com","$2y$08$nTtZU9/UBeRLbYJRO/XwCe2D/B57ofx2bCN8vGEiHkqO.bPra0WT6","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("4","dude12","dude@example.com","$2y$08$y0JweKtWxJFRF7Ko8q0zkODY.EWEKJ.CR1dDco6aCJh8ssKdzQ6RC","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("5","girl1","girl@example.com","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");
Я посмотрел и пробовал методы из этих двух тем, но ничего не работает:
не может запустить-сырец-запрос-в-Laravel-4
Laravel-4-хау в перспективе-а-сырьевой-SQL
Ошибка при выполнении с помощью команды DB :: select (DB :: raw ($ query)) или DB :::
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_use' at line 3 (SQL: DROP TABLE users; CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `u_birthday` date NOT NULL DEFAULT '0000-00-00', `u_lastlogin` int(11) NOT NULL, `u_logcout` int(11) NOT NULL DEFAULT '0', `u_level` tinyint(1) NOT NULL DEFAULT '0', `u_language` tinyint(1) NOT NULL DEFAULT '0', `u_status` tinyint(1) NOT NULL DEFAULT '0', `u_gender` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO users VALUES("1","admin","admin@example.com","$2y$08$9sBJh7iyF9Yr6xvsieNmBOsotGPBkZFydVjb/Yk5Fzh4IGBVo7Je6","0000-00-00 00:00:00","0000-00-00","0","0","9","0","0","0"); INSERT INTO users VALUES("2","moderator","mod@example.com","$2y$08$15/tIKPM/8gATSzkmey5tuAA/PL4lJeFq7liTETyz0h1dkOotwp3G","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("3","helper","help@example.com","$2y$08$nTtZU9/UBeRLbYJRO/XwCe2D/B57ofx2bCN8vGEiHkqO.bPra0WT6","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("4","dude12","dude@example.com","$2y$08$y0JweKtWxJFRF7Ko8q0zkODY.EWEKJ.CR1dDco6aCJh8ssKdzQ6RC","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("5","girl1","girl@example.com","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); ) (Bindings: array ( ))
Выполнение SQL-кода, аналогичное тому, которое я предоставил в вопросе, может быть выполнено с использованием
DB::unprepared( $code );
Надеюсь, это поможет людям с тем же вопросом, что и я: D
Ответ был найден при просмотре проблем с laravel
Отбросьте таблицу пользователей из базы данных, затем попробуйте это и сообщите мне, работает ли она или нет:
DB::insert('CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `u_birthday` date NOT NULL DEFAULT '0000-00-00', `u_lastlogin` int(11) NOT NULL, `u_logcout` int(11) NOT NULL DEFAULT '0', `u_level` tinyint(1) NOT NULL DEFAULT '0', `u_language` tinyint(1) NOT NULL DEFAULT '0', `u_status` tinyint(1) NOT NULL DEFAULT '0', `u_gender` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'); //and so on for all the other queries.
Как насчет этого кода? Мне очень полезно использовать migrations & db seeding – особенно для развертывания.
Schema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
иSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
поSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
поSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
поSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
поSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);
поSchema::create('users', function($table){ $table->increments('id'); $table->string('u_username'); $table->string('u_email'); $table->string('password'); $table->datetime('u_regdate'); $table->date('u_birthday'); $table->integer('u_lastlogin'); $table->integer('u_logcout')->default(0); $table->tinyinteger('u_level')->default(0); $table->tinyinteger('u_language')->default(0); $table->tinyinteger('u_status')->default(0); $table->tinyinteger('u_gender')->default(0); }); // Repeat this for other users as well User::create([ 'u_username' => 'admin', 'u_email' => 'admin@example.com', 'password' => Hash::make('users-password'), 'u_regdate' => date('Ymd H:i:s'), 'u_birthday' => '1980-01-01', 'u_lastlogin' => 0, 'u_logcout' => 0, 'u_level' => 9, 'u_language' => 0, 'u_status' => 0, 'u_gender' => 0, ]);