Skip to main content
Version: 1.x

Sub collection

Retrieve documents from a sub collection

all()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->all();

first()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profile = $user->collection('Profile')->first();

It accepts all Methods from Retrieve documents.

firstOrFail()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profile = $user->collection('Profile')->firstOrFail();

It accepts all Methods from Retrieve documents.

get()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->get();

It accepts all Methods from Retrieve documents.

paginate()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->paginate();
warning

Make sure you add the filter fields to the Firestore indexes to avoid errors. Read more about Firestore indexes.

where()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->where(['id', '=', '123456789'])->get();
warning

Make sure you add the filter fields to the Firestore indexes to avoid errors. Read more about Firestore indexes.

orWhere()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->andWhere([['name', '=', 'John'], ['age' => 25]])->get();
warning

Make sure you add the filter fields to the Firestore indexes to avoid errors. Read more about Firestore indexes.

andWhere()

$user = User::where(['id', '=', '123456789'])->firstOrFail();
$profiles = $user->collection('Profile')->andWhere([['name', '=', 'John'], ['age' => 25]])->get();

Create a document in a sub collection

create($data, $fillable, $required, $default, $fieldTypes, $id)

Parameters

  • $data (array) (required) - The data to create the document with
  • $fillable (array) (required) - The fillable fields of the document. See Fillable
  • $required (array) (optional) - The required fields of the document. See Required
  • $default (array) (optional) - The default fields of the document. See Default
  • $fieldTypes (array) (optional) - The field types of the document. See Field Types
  • $primaryKey (string) (optional) - The primaryKey of the document. If not provided, the primary key will be 'id'.

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$fillable = ['name', 'age'];
$required = ['name'];
$default = [];
$fieldTypes = ['name' => 'string', 'age' => 'integer'];
$primaryKey = 'id';

$newProfile = $user->collection('Profile')->create([
'name' => 'John',
'age' => 25
], $fillable, $required, $default, $fieldTypes, $primaryKey);

echo $newProfile->name; // John
echo $newProfile->age; // 25

Update a document in a sub collection

updateSubDocument($data, $fillable, $required, $default, $fieldTypes, $primaryKey)

Parameters

  • $data (array) (required) - The data to update the document with
  • $fillable (array) (required) - The fillable fields of the document. See Fillable
  • $required (array) (optional) - The required fields of the document. See Required
  • $fieldTypes (array) (optional) - The field types of the document. See Field Types
  • $primaryKey (string) (default = 'id') (required if primaryKey is not 'id') - The primary key of the document. If not provided, the primary key of the model will be used.

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$fillable = ['name', 'age'];
$required = ['name'];
$default = [];
$fieldTypes = ['name' => 'string', 'age' => 'integer'];
$primaryKey = 'id';

$profile = $user->collection('Profile')->where(['id', '=', '123456789'])->firstOrFail();

$updatedProfile = $profile->updateSubDocument([
'name' => 'John',
'age' => 25
], $fillable, $required, $fieldTypes, $primaryKey);

echo $updatedProfile->name; // John
echo $updatedProfile->age; // 25

Delete a document in a sub collection

delete()

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$profile = $user->collection('Profile')->where(['id', '=', '123456789'])->firstOrFail();

$profile->delete();

Delete many documents in a sub collection

warning

Deleting many documents in a sub collection can take a long time if there are a lot of documents in the sub collection.

deleteMany()

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$user->collection('Profile')->where(['age', '>', 20])->deleteMany();

Ordering documents in a sub collection

warning

Make sure you add the filter fields to the Firestore indexes to avoid errors. Read more about Firestore indexes.

orderBy($field, $direction)

Parameters

  • $field (string) - The field to order by
  • $direction (string) - The direction to order by. Can be ASC or DESC

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$profiles = $user->collection('Profile')->orderBy('age', 'DESC')->get();

Limiting documents in a sub collection

limit($limit)

Parameters

  • $limit (int) - The limit of documents to retrieve

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$profiles = $user->collection('Profile')->limit(10);

Paginating documents in a sub collection

paginate($limit, $pageName)

Parameters

  • $limit (int) - The limit of documents to retrieve
  • $pageName (string) - The name of the page query string parameter

Example

$user = User::where(['id', '=', '123456789'])->firstOrFail();

$profiles = $user->collection('Profile')->paginate(10, 'page');

Conclusion

That's it! You now know how to use sub collections in your Laravel application. If you have any issues or questions, please open an issue on GitHub.