{"id":833,"date":"2023-09-12T12:52:11","date_gmt":"2023-09-12T12:52:11","guid":{"rendered":"https:\/\/www.devopsconsulting.in\/blog\/?p=833"},"modified":"2023-10-16T13:08:30","modified_gmt":"2023-10-16T13:08:30","slug":"tutorial-laravel-group-by-first-letter-example","status":"publish","type":"post","link":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/","title":{"rendered":"Tutorial: Laravel Group By First Letter Example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;ll give you an illustration of a lambda group by first letter. This article delves deeply into the grouping of letters in Ruby. You&#8217;ve come to the correct place if you&#8217;re looking to see an example of a first-letter user-created Laravel group. I want to share my first letter in Laravel with your group.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll utilize a sample users database in this example, where we wish to group users based on the first letter of the name field. We&#8217;ll make advantage of the groupBy() Laravel eloquent method and the SQL function SUBSTRING(). so let&#8217;s do the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"339\" src=\"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png\" alt=\"\" class=\"wp-image-834\" srcset=\"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png 624w, https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13-300x163.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong> <strong>Install<\/strong> <strong>laravel<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although it&#8217;s not necessary, you can use the following command if you haven&#8217;t already created the Laravel application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer create-project laravel\/laravel exampleapp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Create Controller<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This step involves creating a UserController and using index() to gather records and categorize them according to it. So let&#8217;s use the command below to construct a controller.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:controller UserController<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>app\/Http\/Controllers\/UserController.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n     \nnamespace App\\Http\\Controllers;\n    \nuse Illuminate\\Http\\Request;\nuse App\\Models\\User;\nuse DB;\n    \nclass UserController extends Controller\n{\n    \/**\n     * Display a listing of the resource.\n     *\n     * @return \\Illuminate\\Http\\Response\n     *\/\n    public function index(Request $request)\n    {\n        $users = User::select('name', DB::raw(\"SUBSTRING(name, 1, 1) as first_letter\"))\n                        ->orderBy('name')\n                        ->get()\n                        ->groupBy('first_letter');\n  \n        return view('users', compact('users'));\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: Add Route<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">routes\/web.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n  \r\nuse Illuminate\\Support\\Facades\\Route;\r\n  \r\nuse App\\Http\\Controllers\\UserController;\r\n  \r\n\/*\r\n|--------------------------------------------------------------------------\r\n| Web Routes\r\n|--------------------------------------------------------------------------\r\n|\r\n| Here is where you can register web routes for your application. These\r\n| routes are loaded by the RouteServiceProvider within a group which\r\n| contains the \"web\" middleware group. Now create something great!\r\n|\r\n*\/\r\n  \r\nRoute::get('users', &#91;UserController::class, 'index']);\r\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: Create View File<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">resources\/views\/users.blade.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html>\r\n&lt;head>\r\n    &lt;title>Laravel Group By with First Latter Example&lt;\/title>\r\n    &lt;meta name=\"csrf-token\" content=\"{{ csrf_token() }}\">\r\n    &lt;link href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/twitter-bootstrap\/5.0.1\/css\/bootstrap.min.css\" rel=\"stylesheet\">\r\n&lt;\/head>\r\n&lt;body>\r\n       \r\n&lt;div class=\"container\">\r\n    &lt;h1>Laravel Group By with First Latter Example&lt;\/h1>\r\n  \r\n    @foreach ($users as $letter => $userGroup)\r\n        &lt;h2>{{ $letter }}&lt;\/h2>\r\n        &lt;ul>\r\n            @foreach ($userGroup as $user)\r\n                &lt;li>{{ $user->name }}&lt;\/li>\r\n            @endforeach\r\n        &lt;\/ul>\r\n    @endforeach\r\n&lt;\/div>\r\n       \r\n&lt;\/body>\r\n      \r\n&lt;\/html><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then run this command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan serve<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll give you an illustration of a lambda group by first letter. This article delves deeply into the grouping of letters in Ruby. You&#8217;ve come to the&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[171],"class_list":["post-833","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel-group-by-first-letter"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial: Laravel Group By First Letter Example - DevOps Consulting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: Laravel Group By First Letter Example - DevOps Consulting\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ll give you an illustration of a lambda group by first letter. This article delves deeply into the grouping of letters in Ruby. You&#8217;ve come to the...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Consulting\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-12T12:52:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-16T13:08:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png\" \/>\n<meta name=\"author\" content=\"Abhishek Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/\"},\"author\":{\"name\":\"Abhishek Singh\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"headline\":\"Tutorial: Laravel Group By First Letter Example\",\"datePublished\":\"2023-09-12T12:52:11+00:00\",\"dateModified\":\"2023-10-16T13:08:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/\"},\"wordCount\":189,\"commentCount\":1,\"image\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/image-13.png\",\"keywords\":[\"Laravel Group By First Letter\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/\",\"name\":\"Tutorial: Laravel Group By First Letter Example - DevOps Consulting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/image-13.png\",\"datePublished\":\"2023-09-12T12:52:11+00:00\",\"dateModified\":\"2023-10-16T13:08:30+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/tutorial-laravel-group-by-first-letter-example\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/image-13.png\",\"contentUrl\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/image-13.png\",\"width\":624,\"height\":339},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/\",\"name\":\"DevOps Consulting\",\"description\":\"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\",\"name\":\"Abhishek Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"caption\":\"Abhishek Singh\"},\"description\":\"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/author\\\/abhishek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: Laravel Group By First Letter Example - DevOps Consulting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial: Laravel Group By First Letter Example - DevOps Consulting","og_description":"I&#8217;ll give you an illustration of a lambda group by first letter. This article delves deeply into the grouping of letters in Ruby. You&#8217;ve come to the...","og_url":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/","og_site_name":"DevOps Consulting","article_published_time":"2023-09-12T12:52:11+00:00","article_modified_time":"2023-10-16T13:08:30+00:00","og_image":[{"url":"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png","type":"","width":"","height":""}],"author":"Abhishek Singh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abhishek Singh","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#article","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/"},"author":{"name":"Abhishek Singh","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"headline":"Tutorial: Laravel Group By First Letter Example","datePublished":"2023-09-12T12:52:11+00:00","dateModified":"2023-10-16T13:08:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/"},"wordCount":189,"commentCount":1,"image":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png","keywords":["Laravel Group By First Letter"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/","url":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/","name":"Tutorial: Laravel Group By First Letter Example - DevOps Consulting","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#primaryimage"},"image":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png","datePublished":"2023-09-12T12:52:11+00:00","dateModified":"2023-10-16T13:08:30+00:00","author":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsconsulting.in\/blog\/tutorial-laravel-group-by-first-letter-example\/#primaryimage","url":"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png","contentUrl":"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2023\/10\/image-13.png","width":624,"height":339},{"@type":"WebSite","@id":"https:\/\/www.devopsconsulting.in\/blog\/#website","url":"https:\/\/www.devopsconsulting.in\/blog\/","name":"DevOps Consulting","description":"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsconsulting.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f","name":"Abhishek Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","caption":"Abhishek Singh"},"description":"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.","url":"https:\/\/www.devopsconsulting.in\/blog\/author\/abhishek\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/comments?post=833"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/833\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}