Skip to content

Instantly share code, notes, and snippets.

@InToSSH
Last active June 17, 2023 00:33
Show Gist options
  • Save InToSSH/ba4cb4d196229df729bdb3568758883c to your computer and use it in GitHub Desktop.
Save InToSSH/ba4cb4d196229df729bdb3568758883c to your computer and use it in GitHub Desktop.

Revisions

  1. InToSSH revised this gist Jun 17, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions eloquent_macro.php
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@
    $this->select($query_table . '.*')
    ->join($relation_table, $query_table . '.' . $relation_foreign_key, '=', $relation_table . '.id')
    ->orderBy($relation_table . '.' . $column, $dir);

    return $this;
    });
  2. InToSSH revised this gist Apr 20, 2021. No changes.
  3. InToSSH created this gist Apr 19, 2021.
    11 changes: 11 additions & 0 deletions eloquent_macro.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?php
    Builder::macro('orderByRelation', function(string $searchColumn, string $dir) {
    list($relation, $column) = explode('.', $searchColumn);
    $relation_table = $this->getRelation($relation)->getModel()->getTable();
    $relation_foreign_key = $this->getRelation($relation)->getForeignKeyName();
    $query_table = $this->getModel()->getTable();

    $this->select($query_table . '.*')
    ->join($relation_table, $query_table . '.' . $relation_foreign_key, '=', $relation_table . '.id')
    ->orderBy($relation_table . '.' . $column, $dir);
    });