Skip to content

Collection

mbroersen edited this page Oct 15, 2020 · 2 revisions

Class

import {Collection} from 'jeloquent';

Collection extends Array

Methods

pluck(): Array

User.find([1,2,3]).pluck('name');

//returns array of user names
['User 1', 'User 2', 'User 3']

first(): Model

User.all().first();

//returns Model
User

last(): Model

User.all().first();

//returns Model
User

merge(collection): Collection

User.find([1,2,3]).merge(User.find([4, 5, 6]));

//returns merged collection
Collection(6) [User, User, User, User, User, User]

where(field, operator, value): Collection

field string name of field

operator <|<=|>|>=|==|!=

value value of field to use operator with

User.all().whereBetween('id', '>', 5);

//returns Collection with user id greater then 5
Collection(4) [User, User, User, User];

whereIn(field, values): Collection

User.all().whereIn('id', [2,4,6]);

// returns collection of users with ids 2,4,6
Collection(3) [User, User, User]

whereNotIn(field, values): Collection

whereBetween(field, values): Collection

User.all().whereBetween('id', [5, 9]);

//returns Collection with user with id between 5 and 9 
Collection(5) [User, User, User, User, User];

whereNotBetween(field, values): Collection

whereNull(field): Collection

User.all().whereNull('team_id');

//returns Collection with user where field team_id has a null value
Collection(1) [User];

whereNotNull(field): Collection

whereInstanceOf(class): Collection

User.find([1,2,3]).merge(Team.find([4, 5, 6])).whereInstanceOf(User);

//returns Collection with only instances of User
Collection(3) [User, User, User];

whereNotInstanceOf(class): Collection