firelyu
firelyu

Reputation: 2222

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');
});

Upvotes: 14

Views: 62749

Answers (4)

ibrahim saputra
ibrahim saputra

Reputation: 417

If you want to partially raw query you can do this

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

Upvotes: 7

Mohammad Ilbeygi
Mohammad Ilbeygi

Reputation: 185

You can use this way for raw insert ( I test this on laravel/Lumen 8 ) :

DB::insert('insert into users (email, votes) values (?, ?)', ['[email protected]', '0']);

Upvotes: 5

sidneydobber
sidneydobber

Reputation: 2910

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 = '[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.

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.

Upvotes: 2

Jeremy
Jeremy

Reputation: 2536

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

I hope this is what you need.

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

Upvotes: 18

Related Questions