Tất cả các phương thức trong Eloquent ORM mà trả về nhiều hơn một bản ghi thì Laravel sẽ trả về một instance collection của Illuminate\Database\Eloquent\Collection
class, bao gồm cả truy vấn đến relationship.
Eloquent Collection này được kế thừa Base collection của Laravel và sẽ bổ sung thêm một số các phương thức khác để hỗ trợ xử lý thêm đối với eloquent model
1. Các phương thức bổ sung
contains
Dùng để xác định xem một model có xuất hiện trong collection
hay không. Phương thức nhận vào là một primary key
hoặc là một instance model
use App\Models\User;
$users = User::where('active', 1)->get();
$users->contains(1);
$users->contains(User::find(1));
diff
Trả về tất cả các model không xuất hiện trong collection
use App\Models\User;
$users = User::where('active', 1)->get();
$users = $users->diff(User::whereIn('id', [1, 2, 3])->get());
except
Trả về tất cả các model không xuất hiện trong array key truyền vào
use App\Models\User;
$users = User::where('active', 1)->get();
$users = $users->except([1, 2, 3]);
find
Trả về tất cả các model xuất hiện trong collection với điều kiện truyền vào phương thức. Giá trị truyền vào có thể là một giá trị của primary key, array hoặc instance model
$users = User::all();
$user = $users->find(1);
// hoặc
$users = User::all();
$user = $users->find([1, 2, 3]);
// hoặc
$users = User::all();
$user = $users->find(User::find(1));
fresh
Query đến database và làm mới hết các giá trị trong collection. Nếu bạn muốn làm mới một relation cụ thể nào đó thì có thể truyền vào relation name
// Làm mới tất cả dữ liệu trong collection.
$users = $users->fresh();
// Làm mới dữ liệu relation comments trong collection.
$users = $users->fresh('comments');
intersect
Trả về tất cả model có trong một eloquent collection khác
use App\Models\User;
$users = $users->intersect(User::whereIn('id', [1, 2, 3])->get());
load
Thực hiện eager load relationship cho tất cả các model trong collection
$users->load(['comments', 'posts']);
$users->load('comments.author');
loadMissing
Thực hiện eager load relationship data cho các model trong collection chưa được load
$users->loadMissing(['comments', 'posts']);
$users->loadMissing('comments.author');
modelKeys
Trả về một mảng primary key của các model trong collection
$users->modelKeys();
// [1, 2, 3, 4, 5]
makeVisible
Hiển thị (visibale) các thuộc tính (attribute) thường được ‘hidden’ của các model trong collection
$users = $users->makeVisible(['passsword']);
makeHidden
Ngược lại với phương thức makeVisible. Nó sẽ ẩn (hidden) các thuộc tính (attribute) của các model trong collection
$users = $users->makeHidden(['email']);
only
Trả về tất cả các model xuất hiện trong mảng dữ liệu truyền vào, với mảng giá trị truyền vào là giá trị của primary key
data
$users = $users->only([1, 2, 3]);
toQuery
Trả về một query builder object chứa ràng buộc whereIn
trên các primary key
data của các model trong collection
use App\Models\User;
$users = User::where('status', 'VIP')->get();
$users->toQuery()->update([
'status' => 'Administrator',
]);
unique
Loại bỏ các model có cùng giá trị, cùng kiểu giá trị primary key của model trong collection
$users = $users->unique();
2. Custom Collection
Trong một số trường hợp, nếu như bạn không muốn eloquent trả về một Illuminate\Database\Eloquent\Collection
mà bạn muốn trả về một custom collection của bạn. Lúc này bạn có thể khai báo trong phương thức newCollection
của model đó
Ví dụ: Trả về một UserCollection
trong User
model khi query
<?php
namespace App\Models;
use App\Support\UserCollection;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Create a new Eloquent Collection instance.
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public function newCollection(array $models = [])
{
return new UserCollection($models);
}
}
Leave a Comment