Qiita Conference 2025

uhyo (@uhyo)

10ヶ月かけてstyled-components v4からv5にアップデートした話

2
0

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 3 years have passed since last update.

Laravelのクエリビルダについて

Last updated at Posted at 2021-07-23

環境

PHP 7.3.24
Laravel 6.20.30
MAMP

クエリビルダとは

・SQLに近い構文をPHPのメソッドとして使い、DBを操作するもの
・メソッドチェーンで記述可能
・ DB::table(テーブル名)->SQLという書き方をする

使用例

参照するテーブルデータ
スクリーンショット 2021-07-23 10.56.38.png

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{
    public function index() {

      // products テーブルのproduct_nameカラムのデータを取得
      DB::table('products')
      ->select('id', 'product_name')
      ->get()
      ->dd();
    }
}

dd()の実行結果は以下の通りです。
DBのidproduct_nameの値を取得できていることがわかります。

Illuminate\Support\Collection {#384 ▼
  #items: array:9 [▼
    0 => {#385 ▼
      +"id": 1
      +"product_name": "ノート"
    }
    1 => {#389 ▼
      +"id": 2
      +"product_name": "タブレット"
    }
    2 => {#390 ▼
      +"id": 3
      +"product_name": "タピオカ"
    }
    3 => {#391 ▼
      +"id": 4
      +"product_name": "カーテン"
    }
    4 => {#392 ▼
      +"id": 5
      +"product_name": "ピアノ"
    }
    5 => {#393 ▼
      +"id": 6
      +"product_name": "うまい棒"
    }
    6 => {#394 ▼
      +"id": 7
      +"product_name": "炊飯器"
    }
    7 => {#395 ▼
      +"id": 8
      +"product_name": "りんご"
    }
    8 => {#396 ▼
      +"id": 9
      +"product_name": "switch"
    }
  ]
}

参考

公式マニュアル
【 Laravel 】クエリビルダ と Eloquent ORM の違いを整理!

2
0
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?