{"id":19514279,"url":"https://github.com/nejcc/laravel-examples","last_synced_at":"2026-02-07T22:35:23.446Z","repository":{"id":150645094,"uuid":"370925213","full_name":"Nejcc/laravel-examples","owner":"Nejcc","description":"My laravel notes and documentation","archived":false,"fork":false,"pushed_at":"2021-05-26T09:16:58.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-04T13:49:35.118Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nejcc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-26T06:10:39.000Z","updated_at":"2021-05-26T09:17:00.000Z","dependencies_parsed_at":"2023-06-12T04:00:31.999Z","dependency_job_id":null,"html_url":"https://github.com/Nejcc/laravel-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Nejcc/laravel-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nejcc%2Flaravel-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nejcc%2Flaravel-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nejcc%2Flaravel-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nejcc%2Flaravel-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nejcc","download_url":"https://codeload.github.com/Nejcc/laravel-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nejcc%2Flaravel-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29211127,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T22:22:11.602Z","status":"ssl_error","status_checked_at":"2026-02-07T22:22:10.684Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T23:35:45.276Z","updated_at":"2026-02-07T22:35:23.430Z","avatar_url":"https://github.com/Nejcc.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# laravel-examples\n\n## User\n\n### Get data from user\n\nGet object\n\n```php\nauth()-\u003euser()\n```\n\nGet User id\n\n```php\nauth()-\u003euser()-\u003eid\n```\n\n```php\nauth()-\u003eid()\n```\n\nGet User email\n\n```php\nauth()-\u003euser()-\u003eemail\n```\n\nGet User Name\n\n```php\nauth()-\u003euser()-\u003ename\n```\n\n### Package relation\n\n- Spatie\n  permissions [https://spatie.be/docs/laravel-permission/v4/introduction](https://spatie.be/docs/laravel-permission/v4/introduction)\n\n#### Retrive first assigned role of user\n\n```php\nauth()-\u003euser()-\u003egetRoleNames()[0]\n```\n\n### Other helper functions\n\n#### Make \"facade\" singelton of user instance\n\n```php\nif (!function_exists('me')) {\n    /**\n     * @return \\Illuminate\\Contracts\\Auth\\Authenticatable|null\n     */\n    function me()\n    {\n        return cache()-\u003eremember('me_helper', 600, function () {\n            return auth()-\u003euser();\n        });\n    }\n}\n```\n\n### Use case\n\n```php\nme()-\u003eemail\n```\n\n```php\nme()-\u003ename\n```\n\n```php\nme()-\u003eid\n```\n\n## Model - Eloquent related helper\n\n## transforms\n\n#### Implode\n\n```php\nreturn User::all()-\u003epluck('id')-\u003eimplode();\n```\n\n#### Plucking data\n\n```php\nreturn User::all()-\u003epluck('id');\n```\n\n#### Relation loading before\n\n```php\n$user = User::query()\n-\u003ewith(['user_data_relation'])\n-\u003eget();\n```\n\n### Relation loading before with custom\n\nIn this case the last parameter need to be an ID that query know where belong to\n\n```php\n$user = User::query()\n-\u003ewith(['user_data_relation' =\u003e function($query){\n    return $query-\u003eselect('field_1', 'field_2', 'extra_field', 'id');\n}])\n-\u003eget();\n```\n\nIn this case we additionaly add subquery filter to filter out all inactive results.\n\n```php\n$user = User::query()\n-\u003ewith(['user_data_relation' =\u003e function($query){\n    return $query-\u003eselect('field_1', 'field_2', 'extra_field', 'id')-\u003ewhere('is_active', 1);\n}])\n-\u003eget();\n```\n\nExample of multi subquery relationship data\n```php\n$user = User::query()\n-\u003ewith(['posts' =\u003e function($query){\n    return $query-\u003eselect('title', 'content',  'created_at', 'id')\n    -\u003ewhere('is_active', 1)\n    -\u003ewith([\n        'comments' =\u003e function($query){\n            return $query-\u003eselect('comment', 'created_at', 'post_id', 'id');\n        }\n    ]);\n}])\n-\u003eget();\n```\n\n#### Relation loading after\n\n```php\n$user = User::query()-\u003eget();\n$user-\u003eload('user_data_relation');\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnejcc%2Flaravel-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnejcc%2Flaravel-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnejcc%2Flaravel-examples/lists"}