14

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');
});
2
  • laravel.com/docs/database Commented Apr 30, 2014 at 3:17
  • 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. This doesn't answer your question but will give you foundation for learning Laravel. Commented Apr 30, 2014 at 6:54

4 Answers 4

18

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

Sign up to request clarification or add additional context in comments.

Comments

7

If you want to partially raw query you can do this

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

1 Comment

Please add "raw" after colon. DB::raw('table') ->insert(['id'=>1, 'date'=>DB::raw('CURDATE()') ]);
5

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

1 Comment

How to insert multiple rows?, I added more arrays, but the rest were ignored (not inserted) like so: ` DB::insert('insert into users (email, votes) values (?, ?)', ['[email protected]', '0'],['[email protected]', '1'],['[email protected]', '2']);` ?
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.