Skip to content

Instantly share code, notes, and snippets.

@akahmet
Created July 19, 2019 12:42
Show Gist options
  • Save akahmet/197e973331358a22865bdc2d10c93ad5 to your computer and use it in GitHub Desktop.
Save akahmet/197e973331358a22865bdc2d10c93ad5 to your computer and use it in GitHub Desktop.
Laravel eloquent patch for parameter suppport for with method
47a48,54
> * The relationships with parameters that should be eager loaded.
> *
> * @var array
> */
> protected $eagerLoadWithParameters = [];
>
> /**
537c544,553
< if (strpos($name, '.') === false) {
---
> //
> preg_match_all('/(.*?)((\[(.*?)\])?)(\.|$)/m', $name, $matches, PREG_SET_ORDER, 0);
> array_pop($matches);
> if(count($matches) == 1){
> $match = $matches[0];
> $parsed_name = $name;
> if (count($match) == 5){
> $parsed_name = $match[1];
> }
>
557a574,575
>
>
560c578
< $relation->addEagerConstraints($models);
---
> $relation -> addEagerConstraints($models);
562d579
< $constraints($relation);
563a581
> $constraints($relation);
584c602,612
< $relation = Relation::noConstraints(function () use ($name) {
---
> preg_match('/(.*?)((\[(.*?)\])?)(\.|$)/m', $name, $matches);
> array_pop($matches);
>
> $parameters = [];
> $parsed_name = $name;
> if (count($matches) == 5){
> $parsed_name = $matches[1];
> $parameters = isset($matches[4]) ? explode(',', $matches[4]) : [];
> }
>
> $relation = Relation::noConstraints(function () use ($parsed_name, $parameters) {
586c614
< return $this->getModel()->newInstance()->$name();
---
> return $this->getModel()->newInstance()->$parsed_name(...$parameters);
588c616
< throw RelationNotFoundException::make($this->getModel(), $name);
---
> throw RelationNotFoundException::make($this->getModel(), $parsed_name);
1186,1187c1214,1218
< foreach (explode('.', $name) as $segment) {
< $progress[] = $segment;
---
> preg_match_all('/(.*?)((\[(.*?)\])?)(\.|$)/m', $name, $segments, PREG_SET_ORDER, 0);
> array_pop($segments);
> $this -> eagerLoadWithParameters = $segments;
> foreach ($segments as $segment) {
> $progress[] = $segment[1].$segment[2];
1195d1225
<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment