forked from gekosale/Gekosale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Przeniesienie flattenCollection do CustomCollection z Helpera
- Loading branch information
1 parent
1483ea5
commit 6a6f923
Showing
9 changed files
with
67 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Gekosale\Core\Model; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
class CustomCollection extends Collection | ||
{ | ||
/** | ||
* Flatten Collection to key-value pairs used in forms | ||
* | ||
* @param $idKey | ||
* @param $translationPath | ||
* @param $language | ||
* | ||
* @return array | ||
*/ | ||
|
||
public function toSelect($idKey, $translationPath, $language) | ||
{ | ||
$items = $this; | ||
$select = []; | ||
$translationPath = explode('.', $translationPath); | ||
|
||
if (count($translationPath) == 2) { | ||
$translationNode = $translationPath[0]; | ||
$translationKey = $translationPath[1]; | ||
} else { | ||
$translationNode = null; | ||
$translationKey = $translationPath[0]; | ||
} | ||
|
||
$items->each(function ($item) use (&$select, $idKey, $translationNode, $translationKey, $items, $language) { | ||
|
||
if (null === $translationNode) { | ||
$select[$item->$idKey] = $item->$translationKey; | ||
} else { | ||
foreach ($item->$translationNode as $translation) { | ||
if ($translation->language_id === $language) { | ||
$select[$item->$idKey] = $translation->$translationKey; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return $select; | ||
} | ||
} |
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
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
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
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
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