Skip to content

Function Find

basic model retrieval

typescript
const factory = new FedacoTestUser();
await factory.NewQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await factory.NewQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
ReferenceLooks LikeValue
await factory.NewQuery().where('email', 'linbolen@gradii.com').doesntExist()exactly matchfalse
await factory.NewQuery().where('email', 'mohamed@laravel.com').doesntExist()exactly matchtrue
ReferenceLooks LikeValue
model.emailexactly match'linbolen@gradii.com'
model.email !== undefinedexactly matchtrue
ReferenceLooks LikeValue
friends !== undefinedexactly matchtrue
friendsmatch[]
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch1
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch2
ReferenceLooks LikeValue
missingexactly matchUndefined();
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match0
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match2
typescript
// .cursor();
for (const m of models) {
  expect(m.id).toEqual(1);
  expect(m.getConnectionName()).toBe('default');
}

see also prerequisites

belongs to many custom pivot

typescript
const john = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 1,
  name: 'John Doe',
  email: 'johndoe@example.com'
});
const jane = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 2,
  name: 'Jane Doe',
  email: 'janedoe@example.com'
});
const jack = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 3,
  name: 'Jack Doe',
  email: 'jackdoe@example.com'
});
const jule = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 4,
  name: 'Jule Doe',
  email: 'juledoe@example.com'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 1,
  level: 'acquaintance'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 2,
  level: 'friend'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 3,
  level: 'bff'
});
await john.NewRelation('friends').attach(jane, {
  friend_level_id: 1
});
await john.NewRelation('friends').attach(jack, {
  friend_level_id: 2
});
await john.NewRelation('friends').attach(jule, {
  friend_level_id: 3
});
const johnWithFriends = await FedacoTestUserWithCustomFriendPivot.createQuery()
  .with('friends')
  .find(1);
ReferenceLooks LikeValue
await (await johnWithFriends.friends.find(it => it.id === 3).getAttribute( 'pivot').level).levelexactly match'friend'
(await johnWithFriends.friends.find(it => it.id === 4).getAttribute( 'pivot').friend).nameexactly match'Jule Doe'

see also prerequisites

check and create methods on multi connections

typescript
await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await FedacoTestUser.useConnection('second_connection').find(
  FedacoTestUser.useConnection('second_connection').insert({
    id: 2,
    email: 'tony.stark@gradii.com'
  })
);
let user1 = await FedacoTestUser.useConnection('second_connection').findOrNew(
  1
);
let user2 = await FedacoTestUser.useConnection('second_connection').findOrNew(
  2
);
ReferenceLooks LikeValue
user2._existsexactly matchtrue
user1.getConnectionName()exactly match'second_connection'
user2.getConnectionName()exactly match'second_connection'
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrNew({
  email: 'tony.stark@gradii.com'
});
ReferenceLooks LikeValue
user2._existsexactly matchtrue
user1.getConnectionName()exactly match'second_connection'
user2.getConnectionName()exactly match'second_connection'
await FedacoTestUser.useConnection('second_connection').count()match1
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrCreate({
  email: 'tony.stark@gradii.com'
});
ReferenceLooks LikeValue
user2.getConnectionName()exactly match'second_connection'
await FedacoTestUser.useConnection('second_connection').count()match2

see also prerequisites

is after retrieving the same model

typescript
const saved = await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
const retrieved = await FedacoTestUser.createQuery().find(1);

see also prerequisites

Released under the MIT License.