Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erickfirmo/phpmodel
:elephant: MySQL Query Builder in PHP
https://github.com/erickfirmo/phpmodel
Last synced: about 1 month ago
JSON representation
:elephant: MySQL Query Builder in PHP
- Host: GitHub
- URL: https://github.com/erickfirmo/phpmodel
- Owner: erickfirmo
- Created: 2020-01-26T18:26:18.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-27T10:55:39.000Z (3 months ago)
- Last Synced: 2024-09-28T15:42:14.998Z (about 2 months ago)
- Language: PHP
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PhpModel
MySQL Query Builder in PHP - by Erick Firmo - https://erickfirmo.dev
## Requirements
- PHP >= 7.4## Install
Install with composer:```sh
composer require erickfirmo/phpmodel
```### Namespace
```php
insert([
'name' => $name,
'company' => $company,
'year' => $year,
'plate' => $plate,
'color' => $color,
]);
// Select register example, returns collection
$cars = (new Car())->select()
->where('year', '=', $year)
->get();```
## Collections```json
{
"model": "App\\Models\\Car",
"table": "cars",
"attributes": [
"id",
"name",
"company",
"year",
"plate",
"uf",
"color",
"price"
],
"items": [
{
"id": "12",
"name": "Fusca",
"company": "VW",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "yellow",
"price": "89000"
},
{
"id": "13",
"name": "Uno",
"company": "Fiat",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "red",
"price": "89000"
},
{
"id": "14",
"name": "Chevette",
"company": "Chevrolet",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "black",
"price": "89000"
},
],
"pages": [
],
}```
## Query
Methods that facilitate the execution of mysql queries in the database:#### Select
Select all columns from the table using the `select` method. Use the `get` method to perform a query and return a collection:
```php
select()
->get();```
Select specific columns from the table passing an array as parameter in `select` method. Use the `get` method to perform a query:
```php
select(['name', 'company', 'year'])
->get();```
#### Where
Adding where clause to query builder:
```php
select()
->where('company', '=', $company)
->get();```
Adding multiple where clause to query builder:
```php
select()
->where('company', '=', $company)
->where('year', '=', $year)
->get();```
#### Insert
Inserting record into database table:```php
insert([
'name' => $name,
'company' => $company,
'plate' => $plate,
'year' => $year,
'color' => $color,
]);```
#### Update
Updating register into database table:
```php
update($id, [
'plate' => $plate,
'color' => $color,
]);```
#### Delete
Deleting register into database table:
```php
delete($id);```
#### FindById
Searching register by id:```php
findById($id);```
#### OrderBy
You can configure the ordering as ascending or descending using the words `asc` or `desc` as parameter in `orderBy` method.Ordering as ascending:
```php
select()
->orderBy('asc')
->get();
```
Ordering as descending:```php
select()
->orderBy('desc')
->get();```
#### Limit
Limiting number of records in the query:
```php
select()
->limit(50)
->get();```
#### PaginationWe can paginate records using the `paginate` method. We must pass the desired number of records per page as a parameter. This method has a value of 10 by default.
In this example, we have 100 records, and we'll display 25 per page:
```php
select()
->paginate(25);```
By default, the `pages` attribute of the collections will be an array with the number of pages:```json
"pages": [
1,
2,
3,
4
],```
We can use this array to create our paging component. Simple example of page component in with php and bootstrap:
```php
```