简体   繁体   English

如何在laravel的mysql数据库中插入原始数据

[英]How to insert raw data in mysql database in laravel

I am the freshman in laravel development.我是laravel开发的大一新生。 I use the mysql database, and want to insert the raw data in the database.我用的是mysql数据库,想在数据库中插入原始数据。 I add the column with $table->binary() .我用$table->binary()添加列。 And how can I insert the data to the column.以及如何将数据插入到列中。

Schema::table('users', function($table)
{
    $table->binary('raw');
});

Try this:尝试这个:

Suppose you have the following tables (called 'users')假设您有以下表(称为“用户”)

id|user|fname|lname|email|etc..
-------------------------
1|jdoe|john|doe|jdoe@example.com|blah....

DB::table('users')->insert(
    array('user' => 'jdoe',
          'fname' => 'john',
          'lname' => 'doe',
          'email' => 'jdoe@example.com')
);

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基于此文档: http : //laravel.com/docs/queries#inserts

You might want to have a look at Eloquent, the default ORM that Laravel uses.你可能想看看 Eloquent,Laravel 使用的默认 ORM。 It makes with databases very easy.它使数据库变得非常容易。 Some people might think differently, but this can be solved through interfaces and repositories.有些人可能会有不同的想法,但这可以通过接口和存储库来解决。

Laravel uses the MVC paradigm. Laravel 使用 MVC 范式。 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.因此,通过用户提交的视图,假设是一个新用户,控制器获取发布请求并将数据传递给模型,然后模型更新数据库。

So let's say you will use the Laravel default User model.因此,假设您将使用 Laravel 默认的 User 模型。 This model allready extends from Eloquent.该模型已经从 Eloquent 扩展而来。 You should already have a database with:您应该已经有一个数据库:

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 = 'john@johndoe.com';
   $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.如果您正确设置了数据库并迁移了 User 表,这应该可以工作,并为您与 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.我开始以这种方式学习 Laravel,它确实可以帮助您正确理解框架。

PS: PS:

DB::query('insert into users (username, email, password) values ("johndoe", "john@johndoe.com", "password")');

This is highly unsafe and vulnerable to SQL injection, but Laravel offers solutions for this, also for raw queries.这是非常不安全的并且容易受到 SQL 注入的影响,但 Laravel 提供了解决方案,也适用于原始查询。

您可以将这种方式用于原始插入(我在 laravel/Lumen 8 上对此进行了测试):

DB::insert('insert into users (email, votes) values (?, ?)', ['john@example.com', '0']);

If you want to partially raw query you can do this如果你想部分原始查询,你可以这样做

DB::('table')
->insert([
   'id'=>1,
   'date'=>DB::raw('CURDATE()')
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM