mysql insert query laravel

In this tutorial learn laravel insert MYSQL query and select query in laravel. 

Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server. To see how read / write connections should be configured, let's look at this example:


'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'root',
    'charset'   => 'utf8',
    'collation' => 'utf8_general_ci',
    'prefix'    => '',
],



Once configured the database connection, you may run queries using facade.

Select Query

$results = DB::select('select * from users where id = ?', [4]);
The select method will always return an array of results.
select records using get() method.  
public function index()
    {
        $users = DB::table('users')->get();

        return view('user.index', ['users' => $users]);
    }
You may also execute a query using named bindings:
$results = DB::select('select * from users where id = :id', ['id' => 1]);

Running An Insert Statement

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Jack']);


The query builder provides an insert method for inserting records into the database table. 
DB::table('users')->insert(
    ['email' => 'taylor@example.com', 'status' => 0]
);

If the table has an auto-incrementing id, insert a record use insertGetId and then retrieve the ID:

$id = DB::table('users')->insertGetId(
    ['email' => 'taylor@example.com', 'status' => 0]
);
Read More about Laravel tutorials 

Laravel views
Laravel Pagination with DB
Laravel number count variable in foreach loop


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...