<commit_before><?php

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
use Illuminate\Database\MySqlConnection;

class ManyBelongsTo extends Relation
{
    use SupportsDefaultModels;

    /**
     * The child model instance of the relation.
     */
    protected $child;

    /**
     * The foreign keys of the parent model.
     *
     * @var string
     */
    protected $foreignKeys;

    /**
     * The associated key on the parent model.
     *
     * @var string
     */
    protected $ownerKey;

    /**
     * The name of the relationship.
     *
     * @var string
     */
    protected $relation;

    /**
     * The count of self joins.
     *
     * @var int
     */
    protected static $selfJoinCount = 0;

    /**
     * Create a new belongs to relationship instance.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  \Illuminate\Database\Eloquent\Model  $child
     * @param  string  $foreignKeys
     * @param  string  $ownerKey
     * @param  string  $relation
     * @return void
     */
    public function __construct(Builder $query, Model $child, $foreignKeys, $ownerKey, $relation)
    {
        if (! $child->getConnection() instanceof MySqlConnection) {
            throw new \RuntimeException( 'Relation '.class_basename(static::class).' is only support for MySQL.' );
        }

        $this->ownerKey = $ownerKey;
        $this->relation = $relation;
        $this->foreignKeys = $foreignKeys;

        // In the underlying base relationship class, this variable is referred to as
        // the "parent" since most relationships are not inversed. But, since this
        // one is we will create a "child" variable for much better readability.
        $this->child = $child;

        parent::__construct($query, $child);
    }

    /**
     * Execute the query as a "select" statement.
     *
     * @param  array  $columns
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function get($columns = ['*'])
    {
        $results = parent::get($columns)->keyBy($this->ownerKey);

        return (new Collection(array_map(function ($key) use ($results) {

            return $results->get($key, null);

        }, explode(',', $this->child->{$this->foreignKeys}))))->filter();
    }

    /**
     * Get the results of the relationship.
     *
     * @return mixed
     */
    public function getResults()
    {
        $results= $this->query->get()->keyBy($this->ownerKey);

        if (! $results) {
            return new Collection($this->getDefaultFor($this->parent));
        }

        return new Collection(array_map(function ($key) use ($results) {

            return $results->get($key, null);

        }, explode(',', $this->child->{$this->foreignKeys})));
    }

    /**
     * Set the base constraints on the relation query.
     *
     * @return void
     */
    public function addConstraints()
    {
        if (static::$constraints) {
            // For belongs to relationships, which are essentially the inverse of has one
            // or has many relationships, we need to actually query on the primary key
            // of the related models matching on the foreign keys that's on a parent.
            $table = $this->related->getTable();

            $this->query->whereIn($table.'.'.$this->ownerKey, explode(',', $this->child->{$this->foreignKeys}));
        }
    }

    /**
     * Set the constraints for an eager load of the relation.
     *
     * @param  array  $models
     * @return void
     */
    public function addEagerConstraints(array $models)
    {
        // We'll grab the primary key name of the related models since it could be set to
        // a non-standard name and not "id". We will then construct the constraint for<commit_msg>Fix bug that ManyBelongsTo relation will give a [ 0=>null, ] result when without parent.<commit_after><?php

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
use Illuminate\Database\MySqlConnection;

class ManyBelongsTo extends Relation
{
    use SupportsDefaultModels;

    /**
     * The child model instance of the relation.
     */
    protected $child;

    /**
     * The foreign keys of the parent model.
     *
     * @var string
     */
    protected $foreignKeys;

    /**
     * The associated key on the parent model.
     *
     * @var string
     */
    protected $ownerKey;

    /**
     * The name of the relationship.
     *
     * @var string
     */
    protected $relation;

    /**
     * The count of self joins.
     *
     * @var int
     */
    protected static $selfJoinCount = 0;

    /**
     * Create a new belongs to relationship instance.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  \Illuminate\Database\Eloquent\Model  $child
     * @param  string  $foreignKeys
     * @param  string  $ownerKey
     * @param  string  $relation
     * @return void
     */
    public function __construct(Builder $query, Model $child, $foreignKeys, $ownerKey, $relation)
    {
        if (! $child->getConnection() instanceof MySqlConnection) {
            throw new \RuntimeException( 'Relation '.class_basename(static::class).' is only support for MySQL.' );
        }

        $this->ownerKey = $ownerKey;
        $this->relation = $relation;
        $this->foreignKeys = $foreignKeys;

        // In the underlying base relationship class, this variable is referred to as
        // the "parent" since most relationships are not inversed. But, since this
        // one is we will create a "child" variable for much better readability.
        $this->child = $child;

        parent::__construct($query, $child);
    }

    /**
     * Execute the query as a "select" statement.
     *
     * @param  array  $columns
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function get($columns = ['*'])
    {
        $results = parent::get($columns)->keyBy($this->ownerKey);

        return (new Collection(array_map(function ($key) use ($results) {

            return $results->get($key, null);

        }, explode(',', $this->child->{$this->foreignKeys}))))->filter();
    }

    /**
     * Get the results of the relationship.
     *
     * @return mixed
     */
    public function getResults()
    {
        $results= $this->query->get()->keyBy($this->ownerKey);

        if (! $results) {
            return new Collection($this->getDefaultFor($this->parent));
        }

        $parentChain = $this->child->{$this->foreignKeys};

        $parentChain = $parentChain ? explode(',', $parentChain) : [];

        return new Collection(array_map(function ($key) use ($results) {

            return $results->get($key, null);

        }, $parentChain));
    }

    /**
     * Set the base constraints on the relation query.
     *
     * @return void
     */
    public function addConstraints()
    {
        if (static::$constraints) {
            // For belongs to relationships, which are essentially the inverse of has one
            // or has many relationships, we need to actually query on the primary key
            // of the related models matching on the foreign keys that's on a parent.
            $table = $this->related->getTable();

            $this->query->whereIn($table.'.'.$this->ownerKey, explode(',', $this->child->{$this->foreignKeys}));
        }
    }

    /**
     * Set the constraints for an eager load of the relation.
     *
     * @param  array  $models
     * @return void
     */
    public function addEagerConstraints(array $models)
    {
        // We'll grab the primary key name of the related models since it could be set to
        // a non-standard name and not "id". We will then construct the constraint for
{'fraction_non_alphanumeric': 0.1214999383249044, 'fraction_numerical': 0.00037005057357838907, 'mean_word_length': 2.3039934800325996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '4668271', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2241, 'n_words': 666}