Skip to content

Commit 918ecce

Browse files
committed
implement global scope to access additional properties
1 parent 001d027 commit 918ecce

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace UserFrosting\Sprinkle\ExtendUser\Database\Scopes;
4+
5+
use Illuminate\Database\Eloquent\Scope;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Builder;
8+
9+
class MemberAuxScope implements Scope
10+
{
11+
/**
12+
* Apply the scope to a given Eloquent query builder.
13+
*
14+
* @param \Illuminate\Database\Eloquent\Builder $builder
15+
* @param \Illuminate\Database\Eloquent\Model $model
16+
* @return void
17+
*/
18+
public function apply(Builder $builder, Model $model)
19+
{
20+
$baseTable = $model->getTable();
21+
// Hardcode the table name here, or you can access it using the classMapper and `getTable`
22+
$auxTable = 'members';
23+
24+
// Specify columns to load from base table and aux table
25+
$builder->addSelect(
26+
"$baseTable.*",
27+
"$auxTable.city as city",
28+
"$auxTable.country as country"
29+
);
30+
31+
// Join on matching `member` records
32+
$builder->leftJoin($auxTable, function ($join) use ($baseTable, $auxTable) {
33+
$join->on("$auxTable.id", '=', "$baseTable.id");
34+
});
35+
}
36+
}

0 commit comments

Comments
 (0)