Skip to content

Commit

Permalink
Added support for scopes
Browse files Browse the repository at this point in the history
Signed-off-by: Gertjan Roke <[email protected]>
  • Loading branch information
GertjanRoke committed Sep 22, 2021
1 parent 675894b commit 9a0ae11
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: :vendor_name
github: GertjanRoke
7 changes: 2 additions & 5 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/:vendor_slug/:package_slug/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/:vendor_slug/:package_slug/discussions/new?category=ideas
url: https://github.com/GertjanRoke/laravel-db-model/issues/new
about: Share ideas for new features
- name: Report a bug
url: https://github.com/:vendor_slug/:package_slug/issues/new
url: https://github.com/GertjanRoke/laravel-db-model/issues/new
about: Report a reproducable bug
2 changes: 1 addition & 1 deletion .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Security Policy

If you discover any security related issues, please email author@domain.com instead of using the issue tracker.
If you discover any security related issues, please email g.a.roke90@gmail.com instead of using the issue tracker.
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,68 @@ use GertjanRoke\LaravelDbModel\DBModel;

class Post extends DBModel
{
public $table = 'posts';
// public $table = 'posts';

// public $connection = 'mysql';
}
```
If no table name was given it will guess it based on the class name just like the Eloquent model those.
Same for the connection, if none is set it will use the default connection.

## How to extend custom scopes
It's basically the same as for Eloquent models, you would need to prefix the methods with `scope`
```php
...

class Post extends DBModel
{
public function scopeActive()
{
$this->db->where('active', true);

return $this;
}
}
```

## Some examples

You can easly create short functions for basic where's or even more complex queries so you have it always in one location instead of everywhere in your code base.
```php
<?php

namespace App\Models;

use App\Models\Comment;
use GertjanRoke\LaravelDbModel\DBModel;

class Post extends DBModel
{
public function scopeActive(): self
{
$this->db->where('active', true);

return $this;
}

public function scopeWithLatestComment(): self
{
$postTable = $this->getTable();
$commentTable = (new Comment())->getTable();

$this->db->join($commentTable, "{$commentTable}.post_id", '=', "{$postTable}.id")
->addSelect("{$commentTable}.body");

return $this;
}
}

// Inside your controller
$post = Post::active()->withLatestComment()->latest()->first();

// Keep in mind the order of calling methods doesn't matter as long as the method before returned the builder instance.
// Like this example will return the same result as the query above.
$post = Post::active()->latest()->withLatestComment()->first();

```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"scripts": {
"test": "./vendor/bin/pest --no-coverage",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test-coverage": "./vendor/bin/pest --coverage-html coverage"
},
"config": {
"sort-packages": true
Expand Down
5 changes: 5 additions & 0 deletions src/DBModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function getDB(): Builder
*/
public function __call($method, $parameters)
{
$scopeMethod = (string) Str::of($method)->ucfirst()->prepend('scope');
if (method_exists(static::class, $scopeMethod)) {
return $this->$scopeMethod(...$parameters);
}

$response = $this->db->$method(...$parameters);

return ($response instanceof Builder) ? $this : $response;
Expand Down
12 changes: 12 additions & 0 deletions tests/MethodCallingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@

expect($returned)->toBeInstanceOf(Collection::class);
});

it("doesn't matter in which order you call the query methods", function () {
$grammer = (new Model())->getDB()->grammar;

$query = Model::where('title', 'Hello World')->active()->toSql();

expect($query)->toEndWith($grammer->wrap('title') . ' = ? and ' . $grammer->wrap('active') . ' = ?');

$query = Model::active()->where('title', 'Hello World')->toSql();

expect($query)->toEndWith($grammer->wrap('active') . ' = ? and ' . $grammer->wrap('title') . ' = ?');
});
7 changes: 6 additions & 1 deletion tests/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

class Model extends DBModel
{
public $table = 'models';
public function scopeActive(): self
{
$this->db->where('active', true);

return $this;
}
}
1 change: 1 addition & 0 deletions tests/migrations/ModelTableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function up()
Schema::create('models', function(Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->boolean('active')->default(false);
$table->timestamps();
});
}
Expand Down

0 comments on commit 9a0ae11

Please sign in to comment.