简体   繁体   中英

How to insert raw data in mysql database in laravel

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.

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).

I hope this is what you need.

Based on this Docs: http://laravel.com/docs/queries#inserts

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.

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.

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:

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

class User extends Eloquent implements UserInterface, RemindableInterface {

app/controllers/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. 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.

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.

您可以将这种方式用于原始插入(我在 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()')
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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