Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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();

```
#### Pagination

We 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


    pages as $key => $page) { ?>






```