Last active
April 24, 2020 14:55
-
-
Save lcharette/06eff80e03d51985c202d77c61bf48d2 to your computer and use it in GitHub Desktop.
UserFrosting Sprunje with Relation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserFrosting\Sprinkle\Gaston\Database\Models; | |
use Illuminate\Database\Schema\Builder; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use UserFrosting\Sprinkle\Account\Database\Models\User; | |
use UserFrosting\Sprinkle\Core\Database\Models\Model; | |
use UserFrosting\Sprinkle\UserProfile\Database\Models\Group; | |
/** | |
* Project db model. | |
*/ | |
class Project extends Model | |
{ | |
/** | |
* Client relation | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\HasOne | |
*/ | |
public function client() | |
{ | |
return $this->belongsTo(Group::class); | |
} | |
//... | |
/** | |
* Joins the activity's user, so we can do things like sort, search, paginate, etc. | |
* | |
* @param Builder $query | |
* @return Builder | |
*/ | |
public function scopeJoinClient(Builder $query) | |
{ | |
$query = $query->select('Gaston_projects.*'); | |
$query = $query->leftJoin('groups', 'Gaston_projects.client_id', '=', 'groups.id'); | |
return $query; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script id="projects-table-column-client" type="text/x-handlebars-template"> | |
<td> | |
{{row.client.name}} | |
</td> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserFrosting\Sprinkle\Gaston\Sprunje; | |
use Illuminate\Database\Schema\Builder; | |
use Interop\Container\ContainerInterface; | |
use UserFrosting\Sprinkle\Core\Util\ClassMapper; | |
use UserFrosting\Sprinkle\Core\Sprunje\Sprunje; | |
use UserFrosting\Sprinkle\Gaston\Database\Models\Project; | |
use UserFrosting\Sprinkle\Gaston\Enum\ProjectStatus; | |
class ProjectsSprunje extends Sprunje | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
protected $sortable = [ | |
'name', | |
'number', | |
'client', | |
'status', | |
'updated_at' | |
]; | |
/** | |
* {@inheritDoc} | |
*/ | |
protected $filterable = [ | |
'name', | |
'number', | |
'client', | |
'status' | |
]; | |
/** | |
* @var ContainerInterface The global container object, which holds all your services. | |
*/ | |
protected $ci; | |
/** | |
* @var bool Keep track of whether the users table has already been joined on the query. | |
*/ | |
protected $joinedClient = false; | |
/** | |
* {@inheritDoc} | |
*/ | |
public function __construct(ClassMapper $classMapper, array $options, ContainerInterface $ci) | |
{ | |
$this->ci = $ci; | |
parent::__construct($classMapper, $options); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function baseQuery() | |
{ | |
return Project::with('client'); | |
} | |
/** | |
* Filter LIKE the client info. | |
* | |
* @param Builder $query | |
* @param mixed $value | |
* @return Builder | |
*/ | |
protected function filterClient($query, $value) | |
{ | |
if (!$this->joinedClient) { | |
$query = $query->joinClient(); | |
} | |
$this->joinedClient = true; | |
return $query->like('groups.name', $value); | |
} | |
/** | |
* Sort based on client name. | |
* | |
* @param Builder $query | |
* @param string $direction | |
* @return Builder | |
*/ | |
protected function sortClient($query, $direction) | |
{ | |
if (!$this->joinedClient) { | |
$query = $query->joinClient(); | |
} | |
$this->joinedClient = true; | |
return $query->orderBy('groups.name', $direction); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment