25
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5のデータベース操作

Last updated at Posted at 2015-08-27

##DBファサードを利用
####SELECTクエリーを実行する

$results = DB::select('select * from users where id = ?', [1]);

####INSERT文を実行する

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

####UPDATE文を実行する

DB::update('update users set votes = 100 where name = ?', ['John']);

####DELETE文を実行する

DB::delete('delete from users');

####通常のSQL文を実行する

DB::statement('drop table users');

詳しくは公式ドキュメント
http://readouble.com/laravel/5/0/dev/ja/database.html

##Database: Query Builderを利用(fluentインターフェイス)

####テーブルから全レコードを取得する

$users = DB::table('users')->get();

foreach ($users as $user)
{
    var_dump($user->name);
}

####テーブルから1レコードを取得する

$user = DB::table('users')->where('name', 'John')->first();

var_dump($user->name);

####レコードの1カラムを取得する

$name = DB::table('users')->where('name', 'John')->pluck('name');

詳しくは公式ドキュメント
http://laravel.com/docs/5.1/queries
http://readouble.com/laravel/5/0/dev/ja/database.html

##Eloquentを利用
公式ドキュメント
http://laravel.com/docs/5.1/eloquent
http://readouble.com/laravel/5/0/dev/ja/eloquent.html

25
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?