Laravel ORM 常用的查詢 Function 對應


User::query()
    // and `col` = ?
    ->where('col', 'foo')

    // and `col` > ?
    ->where('col', '>', 'foo')

    // and `col` in (?, ?)
    ->whereIn('col', ['foo', 'bar'])

    // and `col` not in (?, ?)
    ->whereNotIn('col', ['foo', 'bar'])

    // and `col` between ? and ?
    ->whereBetween('col', [0, 10])

    // and `col` not between ? and ?
    ->whereNotBetween('col', [0, 10])

    // -----------------Raw-----------------

    // and 1=1
    ->whereRaw('1=1')

    // and `col` in (?, ?, ?)
    ->whereIntegerInRaw('col', [1, 2, 3])

    // and `col` not in (?, ?, ?)
    ->whereIntegerNotInRaw('col', [1, 2, 3])

    // -----------------Column-----------------

    // and `col1` between `col2` and `col3`
    ->whereBetweenColumns('col1', ['col2', 'col3'])

    // and `col1` not between `col2` and `col3`
    ->whereNotBetweenColumns('col1', ['col2', 'col3'])

    // and `col1` = `col2`
    ->whereColumn('col1', 'col2')

    // and `col1` > `col2`
    ->whereColumn('col1', '>', 'col2')

    // -----------------Date Time-----------------

    // and date(`col`) = ?
    ->whereDate('col', Carbon::today())

    // and date(`col`) > ?
    ->whereDate('col', '>', Carbon::today())

    // and year(`col`) = ?
    ->whereYear('col', 2022)

    // and year(`col`) > ?
    ->whereYear('col', '>', 2022)

    // and month(`col`) = ?
    ->whereMonth('col', 12)

    // and month(`col`) < ?
    ->whereMonth('col', '<', 12)

    // and day(`col`) = ?
    ->whereDay('col', 1)

    // and day(`col`) > ?
    ->whereDay('col', '>', 1)

    // and time(`col`) = ?
    ->whereTime('col', '10:59:59')

    // and time(`col`) > ?
    ->whereTime('col', '>', '10:59:59')

    // -----------------Sub Query-----------------

    // and `col` = (select `id` from `user` where `id` = ?)
    ->where('col', function ($query) {
        $query->select(['id'])->from('user')->where('id', 1);
    })

    // -----------------Nested-----------------

    // and (`col` = ? or `col` = ?)
    ->where(function ($query) {
        $query->where('col', 'foo');
        $query->orWhere('col', 'foo');
    })

    // -----------------Exists-----------------

    // and exists (select `id` from `user` where `id` = ?)
    ->whereExists(function ($query) {
        $query->select(['id'])->from('user')->where('id', 3);
    })

    // -----------------Other-----------------

    // and match (`col1`, `col2`) against (? in natural language mode)
    ->whereFullText(['col1', 'col2'], '123')

    // and (`col1`, `col2`) = (?, ?)
    ->whereRowValues(['col1', 'col2'], '=', ['foo', 'bar'])
    ->toSql()