ホームページ  >  記事  >  php教程  >  LaravelフレームワークデータベースのCURD操作とコヒーレント操作の使用法

LaravelフレームワークデータベースのCURD操作とコヒーレント操作の使用法

高洛峰
高洛峰オリジナル
2016-11-22 12:40:471856ブラウズ

Laravel フレームワーク データベース CURD の操作、一貫性のある操作方法
Laravel フレームワーク データベース CURD の操作性、一貫性のある操作方法 この操作性は非常に便利であり、具体的な詳細は次のとおりです。テキスト紹介。
Laravel は、シンプルでエレガントな PHP Web 開発フレームワーク (PHP Web フレームワーク) です。ヌードルのような乱雑なコードから解放され、完璧なネットワーク APP を構築するのに役立ち、コードの各行が簡潔で表現力豊かになります。
1. テーブル内のすべての行を取得します
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user-> name);
}
テーブルから単一行を取得します
$user = DB::table('users')->where('name', 'John')->first();
var_dump($ user->name);
単一列の行を取得
$name = DB::table('users')->where('name', 'John')->pluck('name');
列値のリストを取得します
$roles = DB::table('roles')->lists('title');
このメソッドは配列タイトルの役割を返します。カスタム キー列を指定して配列を返すこともできます
$roles = DB::table('roles')->lists('title', 'name');

Select 句を指定します
$users = DB ::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()-> get();
$users = DB::table('users')->select('name as user_name')->get();
Select 句が既存のクエリに追加されます
$ query = DB: :table('users')->select('name');
$users = $query->addSelect('age')->get();
where
$users = DB ::table( 'ユーザー')->where('投票', '>', 100)->get();
OR
$users = DB::table('ユーザー')->where ('投票' , '>', 100)->orWhere('name', 'John')->get();

Where Between
$users = DB::table('users')- >whereBetween( '票', array(1, 100))->get();

Where Not Between
$users = DB::table('users')->whereNotBetween('票', array (1, 100) ))->get();

配列を使用した Where In
$users = DB::table('users')->whereIn('id', array(1, 2, 3) )-> get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();

Where Null を使用してレコードを検索する未設定の値の場合
$users = DB::table('users')->whereNull('updated_at')->get();

Order By、Group By、およびHaving
$users = DB::table ('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->
Offset & Limit
$users = DB::table('users')->skip(10)->take(5)->get();
2. Connection
Joins
query Builder も可能です結合ステートメントを記述するために使用されます。次の例を見てください:
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join( 'orders ', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get( );
左結合ステートメント
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
DB ::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id' )->orOn(...);
})
->get();
DB::table('users')
->join('contacts', function($join)
{
$join ->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
-> get( );

3. グループ化
場合によっては、「exists」やネストされたパラメーターのグループ化など、より高度な where 句の作成が必要になる場合があります。 Laravel クエリビルダーはこれらを処理できます:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$ query->where('votes', '>', 100)
->where('title', 'a8093152e673feb7aba1828c43532094', 'Admin');
})
->get();

上記のクエリは次の SQL を生成します:
select * from users where name = 'John' or (votes > 100 and title
a8093152e673feb7aba1828c43532094 'Admin')
Exists Statements
DB::table('users ' )
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get ();
上記のクエリは次の SQL を生成します:
select * from userswhere names (
select 1 fromorders whereorders.user_id = users.id
)

4. 集計
クエリ ビルダーはさまざまな集計メソッドも提供します。統計、最大、最小、平均、合計など。
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB:: table('注文')->min('価格');
$price = DB::table('注文')->avg('価格');
$total = DB::table('ユーザー数')->sum('votes');
生の式
場合によっては、生の式クエリを使用する必要があるかもしれません。これらの式はクエリ文字列に挿入されるため、SQL インジェクション ポイントを作成しないように注意してください。生の式を作成するには、DB:rawmethod:
生の式の使用
$users = DB::table('users) を使用できます。 ')
->select(DB::raw('count(*) as user_count, status'))
->where('status', 'a8093152e673feb7aba1828c43532094', 1)
->groupBy( 'status' )
->get();

列の値を増加または減少させます

DB::table('users')->increment('votes');
DB::table('ユーザー')- >increment('票', 5);
DB::table('ユーザー')->デクリメント('票');
DB::table('ユーザー')->デクリメント( 'votes', 5);

追加の列更新を指定することもできます:

DB::table('users')->increment('votes', 1, array('name' => 'John' ));

Inserts
テーブルにレコードを挿入します
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
) ;

自動インクリメント ID を持つテーブルへのレコードの挿入
テーブルに自動インクリメント ID フィールドがある場合は、insertGetId を使用してレコードを挿入し、ID を取得します:

$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);


注: PostgreSQL の insertGetId メソッドでは、自動インクリメント列の名前が「id」であることが期待されます。
複数のレコードがテーブルに挿入されます
コードは次のとおりです:
DB::table('users')->insert(array(
array('email' => 'taylor@example.com', ' votes' = > 0),
array('email' => 'dayle@example.com', 'votes' => 0),
));

4. テーブル内のレコードを更新します
コードは次のとおりです:
DB::table('users')
->where('id', 1)
->update(array('votes' => 1));

5。削除
テーブルを削除します レコード
のコードは次のとおりです:
DB::table('users')->where('votes', '72ab85c16fd8ed6098b5cdd7c2ff8778delete();

すべて削除テーブル内のレコード
コードは次のとおりです:
DB::table('users')->delete();
テーブルを削除します

コードは次のとおりです:

DB::table('users' )->truncate();
6. 結合
クエリ ビルダーは、2 つのクエリを「結合」する簡単な方法も提供します:

コードは次のとおりです:
$first = DB::table('users') ->whereNull('first_name');
$ users =
DB::table('users')->whereNull('last_name')->union($first)->get();

UnionAll メソッドも使用でき、同じメソッド シグネチャを持ちます。
悲観的ロック
クエリ ビルダーには、SELECT ステートメントを支援するいくつかの「悲観的ロック」機能が含まれています。 SELECT ステートメント「Shared Lock」を実行すると、sharedLock メソッドを使用してクエリを実行できます:
コードは次のとおりです:
DB::table('users')->where('votes', '>',
) 100)->sharedLock ()->get();
SELECT ステートメントで「lock」を更新すると、lockForUpdate メソッドを使用してクエリを実行できます:

コードは次のとおりです:

DB::table(' users')->where('votes' , '>', 100)->lockForUpdate()->get();
7. クエリをキャッシュする
ニーモニックを使用してクエリの結果を簡単にキャッシュできます。 :
コードは次のとおりです:
$users = DB: :table('users')->remember(10)->get();
この例では、クエリの結果は 10 分間キャッシュされます。分。クエリ結果がキャッシュされる場合、クエリ結果はデー​​タベースに対して実行されず、結果はアプリケーションで指定されたデフォルトのキャッシュ ドライバーからロードされます。キャッシュをサポートするドライバーを使用している場合は、キャッシュにタグを追加することもできます:

コードは次のとおりです:

$users = DB::table('users')->cacheTags(array('people' 、'著者' ))->記憶(10)->get();




声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。