FirestoreModel for laravel or lumen
Creating a model
This will create a model, which will connect to the users
table
use Elafries\FirestoreModel\FirestoreModel;
class User extends FirestoreModel {
}
Handle model fields
use Elafries\FirestoreModel\FirestoreModel;
class User extends FirestoreModel {
protected ?string $table = 'user';
protected array $fillable = [
'name', 'age', 'weight'
];
protected array $hidden = [
'password',
];
protected array $secure = [
'password',
];
}
table
This is an optional parameter.
The name will be generated from the lowercase classname joined with underscores.
eg:
ThisIsATable >> this_is_a_table
If you fill the $table
variable the table name will be overriden by its value.
fillable
When you insert to the database these fields will be added and the secure
fields ONLY!
hidden
When you fetch from the database, these fields will be hidden
secure
When you insert/update the database these fields will be encrypted.
When you insert to the database, it will extend the fillable parameters.
Using a model via dependency injection
class UserController extends Controller
{
public function __construct(private User $user) {}
}
Queries
List queries
:array
Fetch all item in the database $this->user->all();
:array
Fetch items in the database with filtering $this->user->where('name', '=', 'test')->get();
:array
Fetch items in the database with multiple where filtering $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->where('weight', '>', '110')
->get();
:array
Fetch items in the database with filtering and with all the hidden properties $this->user->where('name', '=', 'test')->getRaw();
Single result queries
:array
Fetch the first in the database with filtering $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->first();
:array
Fetch the first in the database with filtering and with all the hidden properties $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->firstRaw();
:array
Fetch the first in the database by id $this->user->findById('2asd123a23a');
:array
Fetch the first in the database by id, with all the hidden properties $this->user->findByIdRaw('2asd123a23a');
Existence queries
:int
Count all items in a query $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->count();
:bool
Check if there is at least one result (exists) $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->exists();
:array
Insert $this->user->create([
'name' => 'Bill Buffalo',
'age' => 43,
'weight' => 92,
'password' => 'secret'
]);
Update
:void
Update by id $this->user->updateById('2asd123a23a', [
'age' => 51,
'weight' => 97,
]);
:void
Update all which match the query $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->update([
'age' => 51,
'weight' => 97,
]);
Delete
:void
Delete all which match the query $this->user
->where('name', '=', 'test')
->where('age', '>', '33')
->delete();