php 如何在laravel的mysql数据库中插入原始数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23378998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert raw data in mysql database in laravel
提问by firelyu
I am the freshman in laravel development.
I use the mysql database, and want to insert the raw data in the database. I add the column with $table->binary()
. And how can I insert the data to the column.
我是laravel开发的大一新生。我使用mysql数据库,想在数据库中插入原始数据。我用$table->binary()
. 以及如何将数据插入到列中。
Schema::table('users', function($table)
{
$table->binary('raw');
});
回答by Jeremy
Try this:
尝试这个:
Suppose you have the following tables (called 'users')
假设您有以下表(称为“用户”)
id|user|fname|lname|email|etc..
-------------------------
1|jdoe|john|doe|[email protected]|blah....
DB::table('users')->insert(
array('user' => 'jdoe',
'fname' => 'john',
'lname' => 'doe',
'email' => '[email protected]')
);
The insert() accepts multiple array (which in turns can be mapped to individual columns in your table).
insert() 接受多个数组(依次可以映射到表中的各个列)。
I hope this is what you need.
我希望这是你所需要的。
Based on this Docs: http://laravel.com/docs/queries#inserts
回答by sidneydobber
You might want to have a look at Eloquent, the default ORM that Laravel uses. It makes with databases very easy. Some people might think differently, but this can be solved through interfaces and repositories.
你可能想看看 Eloquent,Laravel 使用的默认 ORM。它使数据库变得非常容易。有些人可能会有不同的想法,但这可以通过接口和存储库来解决。
Laravel uses the MVC paradigm. The database is supposed to be updated through the model. So through the view a user submits let's say a new user, the controllers gets the post request and passes the data to the model and the model updates the database.
Laravel 使用 MVC 范式。数据库应该通过模型更新。因此,通过用户提交的视图,假设是一个新用户,控制器获取发布请求并将数据传递给模型,然后模型更新数据库。
So let's say you will use the Laravel default User model. This model allready extends from Eloquent. You should already have a database with:
因此,假设您将使用 Laravel 默认的 User 模型。该模型已经从 Eloquent 扩展而来。您应该已经有一个数据库:
User table database
用户表数据库
id, auto_increment
username, varchar(255)
password, varchar(255)
email, varchar(255)
created_at (datetime)
updated_at (datetime)
app/models/User.php
应用程序/模型/User.php
class User extends Eloquent implements UserInterface, RemindableInterface {
app/controllers/HomeController.php
应用程序/控制器/HomeController.php
public function showWelcome()
{
$user = new User;
$user->username = 'johndoe';
$user->email = '[email protected]';
$user->password = 'password';
$user->save();
return View::make('hello');
}
If you have setup the database correctly and have migrated the User table this should work and give you a good starting point for interacting with the database in Laravel. This is not the preferred way of adding data to the database, because this action should not be placed inside of the controller, but that would be a next step. I started learning Laravel this way and it really helps you understand the framework properly.
如果您正确设置了数据库并迁移了 User 表,这应该可以工作,并为您与 Laravel 中的数据库交互提供一个良好的起点。这不是将数据添加到数据库的首选方式,因为此操作不应放置在控制器内部,但这将是下一步。我开始以这种方式学习 Laravel,它确实可以帮助您正确理解框架。
PS:
PS:
DB::query('insert into users (username, email, password) values ("johndoe", "[email protected]", "password")');
This is highly unsafe and vulnerable to SQL injection, but Laravel offers solutions for this, also for raw queries.
这是非常不安全的并且容易受到 SQL 注入的影响,但 Laravel 提供了解决方案,也适用于原始查询。