Bài viết này sẽ cung cấp một số ví dụ quan trọng nhất về laravel eloquent select single column to array. bạn có thể thấy laravel lấy các cột cụ thể từ bộ sưu tập. bạn có thể thấy laravel chỉ nhận được một giá trị cột. Tôi sẽ chỉ cho bạn cách lấy một cột từ cơ sở dữ liệu trong laravel eloquent. Hãy bắt đầu với laravel chỉ nhận một giá trị cột
Bạn có thể xem ví dụ dưới đây về cách lấy một giá trị cột trong mảng bằng cách sử dụng phương thức eloquent của laravel. bạn có thể sử dụng ví dụ này với phiên bản laravel 6, laravel 7, laravel 8 và laravel 9
ví dụ 1
Mã điều khiển
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::pluck['name']->toArray[];
dd[$products];
}
}
đầu ra
Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
ví dụ 2
Mã điều khiển
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
đầu ra
Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
Tôi hy vọng nó có thể giúp bạn
Trình tạo truy vấn cơ sở dữ liệu cung cấp giao diện thuận tiện, thông thạo để tạo và chạy các truy vấn cơ sở dữ liệu. Nó có thể được sử dụng để thực hiện hầu hết các hoạt động cơ sở dữ liệu trong ứng dụng của bạn và hoạt động trên tất cả các hệ thống cơ sở dữ liệu được hỗ trợ
Ghi chú. Trình tạo truy vấn Laravel sử dụng liên kết tham số PDO để bảo vệ ứng dụng của bạn khỏi các cuộc tấn công SQL injection. Không cần xóa các chuỗi được truyền dưới dạng ràng buộc
Truy xuất kết quả
Lấy tất cả các hàng từ một bảng
Để bắt đầu một truy vấn trôi chảy, hãy sử dụng phương pháp
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
4 trên mặt tiền $titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
5. Phương thức $titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
4 trả về một phiên bản trình tạo truy vấn trôi chảy cho bảng đã cho, cho phép bạn xâu chuỗi nhiều ràng buộc hơn vào truy vấn và cuối cùng nhận được kết quả. Trong ví dụ này, hãy chỉ $titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
7 tất cả các bản ghi từ một bảng________số 8Giống như các truy vấn thô, phương thức
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
7 trả về một $titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
9 kết quả trong đó mỗi kết quả là một thể hiện của đối tượng PHP 10. Bạn có thể truy cập giá trị của từng cột bằng cách truy cập cột dưới dạng thuộc tính của đối tượngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Truy xuất một hàng/cột từ một bảng
Nếu bạn chỉ cần truy xuất một hàng từ bảng cơ sở dữ liệu, bạn có thể sử dụng phương thức
11. Phương thức này sẽ trả về một đối tượngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
10 duy nhấtArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
4Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Nếu bạn thậm chí không cần toàn bộ hàng, bạn có thể trích xuất một giá trị từ bản ghi bằng phương pháp
13. Phương thức này sẽ trả về giá trị của cột trực tiếpArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
6Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Chunking kết quả từ một bảng
Nếu bạn cần làm việc với hàng nghìn bản ghi cơ sở dữ liệu, hãy cân nhắc sử dụng phương pháp
14. Phương pháp này truy xuất một "khối" kết quả nhỏ tại một thời điểm và đưa từng đoạn vào mộtArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
15 để xử lý. Phương pháp này rất hữu ích để viết các lệnh Artisan xử lý hàng nghìn bản ghi. Ví dụ: hãy làm việc với toàn bộ bảngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
16 theo khối 100 bản ghi cùng một lúcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
0namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Bạn có thể ngừng xử lý các khối tiếp theo bằng cách trả lại
17 từArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
15Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
0namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Truy xuất danh sách các giá trị cột
Nếu bạn muốn truy xuất một mảng chứa các giá trị của một cột, bạn có thể sử dụng phương thức
19. Trong ví dụ này, chúng tôi sẽ truy xuất một mảng các chức danh vai tròArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
Bạn cũng có thể chỉ định cột khóa tùy chỉnh cho mảng được trả về
1Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
uẩn
Trình tạo truy vấn cũng cung cấp nhiều phương pháp tổng hợp, chẳng hạn như
70,namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
71,namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
72,namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
73 vànamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
74. Bạn có thể gọi bất kỳ phương thức nào sau khi xây dựng truy vấn của mìnhnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
7namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Tất nhiên, bạn có thể kết hợp các phương thức này với các mệnh đề khác để xây dựng truy vấn của mình
3Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
chọn
Chỉ định một mệnh đề chọn
Tất nhiên, không phải lúc nào bạn cũng muốn chọn tất cả các cột từ bảng cơ sở dữ liệu. Sử dụng phương thức
75, bạn có thể chỉ định mệnh đềnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
75 tùy chỉnh cho truy vấnnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
6Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
Phương thức
77 cho phép bạn buộc truy vấn trả về các kết quả khác biệtnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
40Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Nếu bạn đã có một phiên bản trình tạo truy vấn và bạn muốn thêm một cột vào mệnh đề chọn hiện có của nó, bạn có thể sử dụng phương thức
78namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
41Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
biểu thức thô
Đôi khi bạn có thể cần sử dụng biểu thức thô trong truy vấn. Các biểu thức này sẽ được đưa vào truy vấn dưới dạng chuỗi, vì vậy hãy cẩn thận để không tạo bất kỳ điểm chèn SQL nào. Để tạo một biểu thức thô, bạn có thể sử dụng phương thức
79namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
42Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
tham gia
Tuyên bố tham gia bên trong
Trình tạo truy vấn cũng có thể được sử dụng để viết các câu lệnh nối. Để thực hiện "nối bên trong" SQL cơ bản, bạn có thể sử dụng phương thức
30 trên phiên bản trình tạo truy vấn. Đối số đầu tiên được truyền cho phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
30 là tên của bảng mà bạn cần nối, trong khi các đối số còn lại xác định các ràng buộc cột cho phép nối. Tất nhiên, như bạn có thể thấy, bạn có thể tham gia nhiều bảng trong một truy vấnArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
43Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Tuyên bố tham gia trái
Nếu bạn muốn thực hiện "nối bên trái" thay vì "nối bên trong", hãy sử dụng phương thức
32. Phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
32 có cùng chữ ký với phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
30Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
44Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Tuyên bố tham gia chéo
Để thực hiện "nối chéo", hãy sử dụng phương thức
35 với tên của bảng bạn muốn nối chéo với. Tham gia chéo tạo ra một sản phẩm cartesian giữa bảng đầu tiên và bảng đã tham giaArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
45Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Tuyên bố tham gia nâng cao
Bạn cũng có thể chỉ định các mệnh đề nối nâng cao hơn. Để bắt đầu, hãy chuyển một
15 làm đối số thứ hai vào phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
30.Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
15 sẽ nhận một đối tượngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
39 cho phép bạn chỉ định các ràng buộc đối với mệnh đềArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
30Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
46Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Nếu bạn muốn sử dụng mệnh đề kiểu "where" trong các phép nối của mình, bạn có thể sử dụng các phương thức
61 vàArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
62 trên một phép nối. Thay vì so sánh hai cột, các phương thức này sẽ so sánh cột với một giá trịArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
47Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
đoàn thể
Trình tạo truy vấn cũng cung cấp một cách nhanh chóng để "kết hợp" hai truy vấn với nhau. Ví dụ: bạn có thể tạo truy vấn ban đầu, sau đó sử dụng phương thức
63 để kết hợp truy vấn đó với truy vấn thứ haiArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
48Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Phương thức
64 cũng có sẵn và có chữ ký phương thức giống nhưArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
63Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
mệnh đề ở đâu
Mệnh đề Where đơn giản
Để thêm mệnh đề
61 vào truy vấn, hãy sử dụng phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
61 trên phiên bản trình tạo truy vấn. Cuộc gọi cơ bản nhất tớiArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
61 yêu cầu ba đối số. Đối số đầu tiên là tên của cột. Đối số thứ hai là một toán tử, có thể là bất kỳ toán tử nào được cơ sở dữ liệu hỗ trợ. Đối số thứ ba là giá trị để đánh giá đối với cộtArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
Ví dụ: đây là truy vấn xác minh giá trị của cột "phiếu bầu" bằng 100
49Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Để thuận tiện, nếu bạn chỉ muốn xác minh rằng một cột bằng với một giá trị nhất định, bạn có thể chuyển trực tiếp giá trị đó làm đối số thứ hai cho phương thức
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
60Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Tất nhiên, bạn có thể sử dụng nhiều toán tử khác khi viết mệnh đề
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
61Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Bạn cũng có thể chuyển một loạt các điều kiện cho hàm
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
62Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Hoặc Tuyên bố
Bạn có thể xâu chuỗi các ràng buộc where lại với nhau, cũng như thêm mệnh đề
402 vào truy vấn. Phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
62 chấp nhận các đối số giống như phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
63Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Các mệnh đề bổ sung
ở đâu giữa
Phương thức
405 xác minh rằng giá trị của cột nằm giữa hai giá trịArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
64Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
whereNotBetween
Phương thức
406 xác minh rằng giá trị của cột nằm ngoài hai giá trịArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
65Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
ở đâu / không ở đâu
Phương thức
407 xác minh rằng giá trị của một cột nhất định được chứa trong mảng đã choArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
63Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Phương thức
408 xác minh rằng giá trị của cột đã cho không có trong mảng đã choArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
67Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
whereNull / whereNotNull
Phương thức
409 xác minh rằng giá trị của cột đã cho làArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
410Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
68Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Phương thức
411 xác minh rằng giá trị của cột không phải làArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
410Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
69Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
ở đâuCột
Phương pháp
413 có thể được sử dụng để xác minh rằng hai cột bằng nhauArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
00namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Bạn cũng có thể chuyển toán tử so sánh cho phương thức
01namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Phương thức
413 cũng có thể được truyền vào một mảng có nhiều điều kiện. Các điều kiện này sẽ được nối bằng cách sử dụng toán tửArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
415Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
02namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Mệnh đề nâng cao
Nhóm tham số
Đôi khi, bạn có thể cần tạo các mệnh đề where nâng cao hơn, chẳng hạn như "nơi tồn tại" hoặc các nhóm tham số lồng nhau. Trình tạo truy vấn của Laravel cũng có thể xử lý những điều này. Để bắt đầu, hãy xem một ví dụ về ràng buộc nhóm trong ngoặc đơn
03namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Như bạn có thể thấy, việc chuyển một
15 vào phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
62 sẽ hướng dẫn trình tạo truy vấn bắt đầu một nhóm ràng buộc.Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
15 sẽ nhận được một phiên bản trình tạo truy vấn mà bạn có thể sử dụng để đặt các giới hạn cần có trong nhóm dấu ngoặc đơn. Ví dụ trên sẽ tạo ra SQL sauArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
04namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Tồn tại báo cáo
Phương thức
419 cho phép bạn viết các mệnh đề SQLArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
420. Phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
419 chấp nhận một đối sốArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
15, đối số này sẽ nhận một phiên bản trình tạo truy vấn cho phép bạn xác định truy vấn sẽ được đặt bên trong mệnh đề "tồn tại"Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
05namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Truy vấn trên sẽ tạo ra SQL sau
06namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Mệnh đề JSON ở đâu
Laravel hỗ trợ truy vấn các loại cột JSON trên cơ sở dữ liệu cung cấp hỗ trợ cho các loại cột JSON. Hiện tại, điều này bao gồm MySQL 5. 7 và Postgres. Để truy vấn một cột JSON, hãy sử dụng toán tử
423Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
07namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Đặt hàng, nhóm, giới hạn và bù trừ
đặt bởi
Phương thức
424 cho phép bạn sắp xếp kết quả của truy vấn theo một cột nhất định. Đối số đầu tiên của phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
424 phải là cột bạn muốn sắp xếp theo, trong khi đối số thứ hai kiểm soát hướng sắp xếp và có thể làArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
426 hoặcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
427Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
63Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
theo thứ tự ngẫu nhiên
Phương thức
428 có thể được sử dụng để sắp xếp kết quả truy vấn một cách ngẫu nhiên. Ví dụ: bạn có thể sử dụng phương pháp này để tìm nạp một người dùng ngẫu nhiênArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
09namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
nhómBy / có / cóRaw
Các phương pháp
429 vàArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
430 có thể được sử dụng để nhóm các kết quả truy vấn. Chữ ký của phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
430 tương tự như chữ ký của phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
00namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Phương thức
433 có thể được sử dụng để đặt một chuỗi thô làm giá trị của mệnh đềArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
430. Ví dụ: chúng tôi có thể tìm thấy tất cả các bộ phận có doanh thu lớn hơn $2.500Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
01namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
bỏ qua / lấy
Để giới hạn số lượng kết quả trả về từ truy vấn hoặc bỏ qua một số kết quả nhất định trong truy vấn [
435], bạn có thể sử dụng các phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
436 vàArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
437Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
02namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Câu điều kiện
Đôi khi bạn có thể muốn các câu lệnh chỉ áp dụng cho một truy vấn khi điều gì đó khác là đúng. Chẳng hạn, bạn có thể chỉ muốn áp dụng câu lệnh
61 nếu một giá trị đầu vào nhất định có trong yêu cầu đến. Bạn có thể thực hiện việc này bằng cách sử dụng phương phápArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
439Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
03namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Phương thức
439 chỉ thực thi Closure đã cho khi tham số đầu tiên làArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
441. Nếu tham số đầu tiên làArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
17 thì Closure sẽ không được thực hiệnArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
phụ trang
Trình tạo truy vấn cũng cung cấp phương thức
443 để chèn bản ghi vào bảng cơ sở dữ liệu. Phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
443 chấp nhận một mảng tên và giá trị cột để chènArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
04namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Bạn thậm chí có thể chèn nhiều bản ghi vào bảng chỉ bằng một lệnh gọi tới
443 bằng cách chuyển một mảng các mảng. Mỗi mảng đại diện cho một hàng được chèn vào bảngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
05namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
ID tăng tự động
Nếu bảng có id tăng tự động, hãy sử dụng phương thức
446 để chèn bản ghi rồi truy xuất IDArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
06namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Ghi chú. Khi sử dụng PostgreSQL, phương thức insertGetId yêu cầu cột tăng tự động có tên là
447. Nếu bạn muốn truy xuất ID từ một "dãy" khác, bạn có thể chuyển tên dãy làm tham số thứ hai cho phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
446Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
cập nhật
Tất nhiên, ngoài việc chèn các bản ghi vào cơ sở dữ liệu, trình xây dựng truy vấn cũng có thể cập nhật các bản ghi hiện có bằng phương pháp
449. Phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
449, giống như phương thứcArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
443, chấp nhận một mảng các cặp cột và giá trị chứa các cột sẽ được cập nhật. Bạn có thể hạn chế truy vấnArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
449 bằng mệnh đềArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
61Array
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
Tăng giảm
Trình tạo truy vấn cũng cung cấp các phương thức thuận tiện để tăng hoặc giảm giá trị của một cột nhất định. Đây chỉ đơn giản là một lối tắt, cung cấp một giao diện rõ ràng và ngắn gọn hơn so với việc viết câu lệnh
449 theo cách thủ côngArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
Cả hai phương thức này đều chấp nhận ít nhất một đối số. cột để sửa đổi. Đối số thứ hai có thể được chuyển tùy ý để kiểm soát số lượng mà theo đó cột sẽ được tăng/giảm
07namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Bạn cũng có thể chỉ định các cột bổ sung để cập nhật trong quá trình hoạt động
08namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
xóa
Tất nhiên, trình tạo truy vấn cũng có thể được sử dụng để xóa các bản ghi khỏi bảng thông qua phương thức
455Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
09namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
Bạn có thể ràng buộc các câu lệnh
455 bằng cách thêm các mệnh đềArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
61 trước khi gọi phương thứcArray
[
[0] => Array
[
[name] => Samsung Galaxy
]
[1] => Array
[
[name] => Apple iPhone 12
]
[2] => Array
[
[name] => Google Pixel 2 XL
]
[3] => Array
[
[name] => LG V10 H800
]
]
455Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
0Nếu bạn muốn cắt bớt toàn bộ bảng, thao tác này sẽ xóa tất cả các hàng và đặt lại ID tăng tự động về 0, bạn có thể sử dụng phương pháp
459Array
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
1khóa bi quan
Trình tạo truy vấn cũng bao gồm một vài hàm để giúp bạn thực hiện "khóa bi quan" trên các câu lệnh
75 của mình. Để chạy câu lệnh với "khóa chia sẻ", bạn có thể sử dụng phương thứcnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response[]
*/
public function index[]
{
$products = Product::select['name']->get[]->toArray[];
dd[$products];
}
}
461 trên một truy vấn. Khóa dùng chung ngăn các hàng đã chọn bị sửa đổi cho đến khi giao dịch của bạn được thực hiệnArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]
$titles = DB::table['roles']->pluck['title'];
foreach [$titles as $title] {
2Ngoài ra, bạn có thể sử dụng phương pháp
462. Khóa "để cập nhật" ngăn không cho các hàng bị sửa đổi hoặc được chọn bằng một khóa dùng chung khácArray
[
[0] => Samsung Galaxy
[1] => Apple iPhone 12
[2] => Google Pixel 2 XL
[3] => LG V10 H800
]