Làm cách nào để lấy giá trị cột trong Laravel?

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ố 8

Giố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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ượng

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

11. Phương thức này sẽ trả về một đối tượng

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

10 duy nhất

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

4

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

13. Phương thức này sẽ trả về giá trị của cột trực tiếp

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

6

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ột

Array

[

[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ảng

Array

[

[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úc

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];

}

}

0

Bạn có thể ngừng xử lý các khối tiếp theo bằng cách trả lại

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

17 từ

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

15

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];

}

}

0

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ò

$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ề

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

1

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ư

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];

}

}

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ì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];

}

}

7

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

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

]

]

3

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

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, 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ấn

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

]

]

6

Phương thức

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];

}

}

77 cho phép bạn buộc truy vấn trả về các kết quả khác biệt

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

40

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

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];

}

}

78

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

41

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

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];

}

}

79

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

42

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

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

]

]

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ứ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

]

]

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ấn

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

43

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

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

]

]

32. Phương thứ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

]

]

32 có cùng chữ ký với phương thứ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

]

]

30

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

44

Tuyên bố tham gia chéo

Để thực hiện "nối chéo", hãy sử dụng phương thứ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

]

]

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 gia

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

45

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

15 làm đối số thứ hai vào phương thứ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

]

]

30.

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

15 sẽ nhận một đối tượng

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

]

]

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

]

]

30

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

46

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

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

]

]

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] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

47

đ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

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

]

]

63 để kết hợp truy vấn đó với truy vấn thứ hai

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

48

Phương thứ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

]

]

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

]

]

63

mệnh đề ở đâu

Mệnh đề Where đơn giản

Để thêm 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

]

]

61 vào truy vấn, hãy sử dụng phương thứ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

]

]

61 trên phiên bản trình tạo truy vấn. Cuộc gọi cơ bản nhất tới

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

]

]

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ột

Ví dụ: đây là truy vấn xác minh giá trị của cột "phiếu bầu" bằng 100

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

49

Để 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

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

]

]

61

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

60

Tất nhiên, bạn có thể sử dụng nhiều toán tử khác khi viết 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

]

]

61

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

61

Bạn cũng có thể chuyển một loạt các điều kiện cho hàm

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

]

]

61

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

62

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 đề

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

402 vào truy vấn. Phương thứ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

]

]

62 chấp nhận các đối số giống như phương thứ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

]

]

61

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

63

Các mệnh đề bổ sung

ở đâu giữa

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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

]

64

whereNotBetween

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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

]

65

ở đâu / không ở đâu

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

407 xác minh rằng giá trị của một cột nhất định được chứa trong mảng đã cho

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

63

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

408 xác minh rằng giá trị của cột đã cho không có trong mảng đã cho

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

67

whereNull / whereNotNull

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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

]

410

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

68

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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

]

410

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

69

ở đâuCột

Phương pháp

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

413 có thể được sử dụng để xác minh rằng hai cột bằng nhau

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];

}

}

00

Bạn cũng có thể chuyển toán tử so sánh cho phương thức

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];

}

}

01

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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

]

415

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];

}

}

02

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

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];

}

}

03

Như bạn có thể thấy, việc chuyển một

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

15 vào phương thứ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

]

]

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] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => 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 sau

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];

}

}

04

Tồn tại báo cáo

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

419 cho phép bạn viết các mệnh đề SQL

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

420. Phương thức

Array

[

[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"

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];

}

}

05

Truy vấn trên sẽ tạo ra SQL sau

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];

}

}

06

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ử

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

423

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];

}

}

07

Đặt hàng, nhóm, giới hạn và bù trừ

đặt bởi

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ức

Array

[

[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ặc

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

427

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

63

theo thứ tự ngẫu nhiên

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ê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];

}

}

09

nhómBy / có / cóRaw

Các phương pháp

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ức

Array

[

[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ứ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

]

]

61

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];

}

}

00

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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.500

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];

}

}

01

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 [

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

435], bạn có thể sử dụng các phương thức

Array

[

[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

]

437

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];

}

}

02

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

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

]

]

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áp

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

439

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];

}

}

03

Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ện

phụ trang

Trình tạo truy vấn cũng cung cấp phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

443 để chèn bản ghi vào bảng cơ sở dữ liệu. Phương thức

Array

[

[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è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];

}

}

04

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ảng

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];

}

}

05

ID tăng tự động

Nếu bảng có id tăng tự động, hãy sử dụng phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

446 để chèn bản ghi rồi truy xuất ID

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];

}

}

06

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à

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

446

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

449. Phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

449, giống như phương thức

Array

[

[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ấn

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

449 bằng 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

]

]

61

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

449 theo cách thủ công

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

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];

}

}

07

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

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];

}

}

08

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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

455

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];

}

}

09

Bạn có thể ràng buộc các câu lệnh

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

455 bằng cách thêm các 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

]

]

61 trước khi gọi phương thức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

455

$titles = DB::table['roles']->pluck['title'];

foreach [$titles as $title] {

0

Nế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

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

459

$titles = DB::table['roles']->pluck['title'];

foreach [$titles as $title] {

1

khó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

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 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ức

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ện

$titles = DB::table['roles']->pluck['title'];

foreach [$titles as $title] {

2

Ngoài ra, bạn có thể sử dụng phương pháp

Array

[

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

]

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ác

Làm cách nào để lấy giá trị cột của bảng trong Laravel?

Ở dòng 17, chúng ta tạo một hàm có tên là columnValue[] sử dụng phương thức plum[] . Ở dòng 18, chúng ta sử dụng phương thức plum[] để lấy các giá trị của cột có tên firstName trong bảng users. Trình tạo truy vấn table[] truy xuất tất cả người dùng trên bảng cơ sở dữ liệu. Kết quả chúng tôi nhận được từ truy vấn là một tập hợp.

Làm cách nào để chỉ nhận giá trị cột trong Laravel?

Chọn các cột cụ thể với Laravel Eloquent . $user = Người dùng. where['tên người dùng', 'bobbyiliev']->get[]; . pass it as an argument to the get[] method.

Làm cách nào để có được một cột trong Laravel?

Bạn có thể lấy giá trị cột đơn từ bộ sưu tập đối tượng bằng cách sử dụng phương thức plum[] với phương thức first[] trong Laravel sau điều kiện where.

Làm cách nào để tìm nạp một giá trị từ cơ sở dữ liệu trong Laravel?

Trên ấu trùng 5. 6 nó có một giải pháp rất đơn giản. Người dùng. where['username', $username]->first[]->groupName ; .

Chủ Đề