<commit_before><?php

/**
 * Deep
 *
 * @package      rsanchez\Deep
 * @author       <NAME> <<EMAIL>>
 */

namespace rsanchez\Deep\Model;

use Illuminate\Database\Eloquent\Model;

/**
 * Model for the categories table
 */
class Category extends Model
{
    /**
     * {@inheritdoc}
     *
     * @var string
     */
    protected $table = 'categories';

    /**
     * {@inheritdoc}
     *
     * @var string
     */
    protected $primaryKey = 'cat_id';
}<commit_msg>Add a nested scope to Category model<commit_after><?php

/**
 * Deep
 *
 * @package      rsanchez\Deep
 * @author       <NAME> <<EMAIL>>
 */

namespace rsanchez\Deep\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

/**
 * Model for the categories table
 */
class Category extends Model
{
    /**
     * {@inheritdoc}
     *
     * @var string
     */
    protected $table = 'categories';

    /**
     * {@inheritdoc}
     *
     * @var string
     */
    protected $primaryKey = 'cat_id';

    /**
     * {@inheritdoc}
     */
    public function newQuery($excludeDeleted = true)
    {
        $query = parent::newQuery($excludeDeleted);

        $query->select($this->table.'.*');

        return $query;
    }

    /**
     * Order by Category Nesting
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNested(Builder $query)
    {
        $connection = $query->getQuery()->getConnection();

        $tablePrefix = $connection->getTablePrefix();

        return $query->leftJoin("{$this->table} AS subcategories", $connection->raw('`subcategories`.`cat_id`'), '=', 'categories.parent_id')
            ->orderBy($connection->raw("coalesce(`subcategories`.`cat_url_title`, `{$tablePrefix}categories`.`cat_url_title`), `{$tablePrefix}categories`.`cat_order`"));
    }
}
{'fraction_non_alphanumeric': 0.16055289739500267, 'fraction_numerical': 0.0, 'mean_word_length': 2.598470363288719, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7211806', 'n_tokens_mistral': 590, 'n_tokens_neox': 587, 'n_words': 108}