{"id":772,"date":"2023-08-17T12:34:31","date_gmt":"2023-08-17T12:34:31","guid":{"rendered":"https:\/\/www.devopsconsulting.in\/blog\/?p=772"},"modified":"2023-10-06T12:35:38","modified_gmt":"2023-10-06T12:35:38","slug":"how-does-laravel-9-use-mysql-view","status":"publish","type":"post","link":"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/","title":{"rendered":"How Does Laravel 9 Use MySQL View?"},"content":{"rendered":"\n<p>You may learn about MySQL views in this blog post and how to successfully incorporate them into Laravel apps.<\/p>\n\n\n\n<p><strong>SQL Create View Query<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW view_data AS\r\r\nSELECT \r\r\n    users.id, \r\r\n    users.name, \r\r\n    users.email,\r\n\r\n    (SELECT count(*) FROM posts\r\r\n                WHERE posts.user_id = users.id\r\r\n            ) AS total_posts,\r\r\n    (SELECT count(*) FROM comments\r\r\n                WHERE comments.user_id = users.id\r\r\n            ) AS total_comments\r\n\r\nFROM users<\/code><\/pre>\n\n\n\n<p><strong>SQL Drop View Query<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP VIEW IF EXISTS `view_data`;<\/code><\/pre>\n\n\n\n<p>\u00a0Let\u2019s create migration with views.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:migration create_view<\/code><\/pre>\n\n\n\n<p><strong>Update Migration File:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r \r\nuse Illuminate\\Database\\Migrations\\Migration;\r\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\r\nuse Illuminate\\Support\\Facades\\Schema;\r\n\r\n  \r\r\nclass CreateView extends Migration\r\r\n{\r\r\n    \/**\r\r\n     * Run the migrations.\r\r\r\r\n     * @return void\r\r\n     *\/\r\r\n    public function up()\r\r\n    {\r\r\n        \\DB::statement($this->createView());\r\r\n    }\r\n\r\r\n    \/**\r\r\n     * Reverse the migrations.\r\r\n     *\r\r\n     * @return void\r\r\n     *\/\r\n\r\n    public function down()\r\n\r\n    {\r\r\n        \\DB::statement($this->dropView());\r\r\n    }\r\n\r\n   \r\n\r\n    \/**\r\r\n     * Reverse the migrations.\r\r\n     *\r\r\n     * @return void\r\r\n     *\/\r\r\n    private function createView(): string\r\r\n    {\r\r\n        return &lt;&lt;\r\r\n            CREATE VIEW view_data AS\r\r\n                SELECT \r\r\n                    users.id, \r\r\n                    users.name, \r\r\n                    users.email,\r\r\n                    (SELECT count(*) FROM posts\r\r\n                                WHERE posts.user_id = users.id\r\r\n                            ) AS total_posts,\r\r\n                    (SELECT count(*) FROM comments\r\r\n                               WHERE comments.user_id = users.id\r\r\n                            ) AS total_comments\r\r\n                FROM users\r\r\n            SQL;\r\r\n    }\r\r\n    \/**\r\r\n     * Reverse the migrations.\r\r\n     *\r\r\n     * @return void\r\r\n     *\/\r\r\n    private function dropView(): string\r\r\n    {\r\r\n        return &lt;&lt;\r\r\n            DROP VIEW IF EXISTS `view_data`;\r\r\n            SQL;\r\r\n    }\r\r\n}<\/code><\/pre>\n\n\n\n<p><strong>now we will create model as below:<\/strong><\/p>\n\n\n\n<p><strong>app\/ViewData.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r \r\nnamespace App;\r\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\n \r\nclass ViewUserData extends Model\r\r\n{\r\r\n    public $table = \"view_data\";\r\r\n}<\/code><\/pre>\n\n\n\n<p><strong>Now we can use it as below on the controller file:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse App\\ViewData;\n\n  \n\nclass UserController extends Controller\n\n{\n    \/**\n     * Display a listing of the resource.\n     *\n     * @return \\Illuminate\\Http\\Response\n     *\/\n\n    public function index()\n    {\n        $users = ViewData::select(\"*\")\n                        ->get()\n                        ->toArray();   \n        dd($users);\n\n    }\n\n}<\/code><\/pre>\n\n\n\n<p><strong>you can see output:-<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array:20 &#91;\r\r\n  0 => array:5 &#91;\r\r\n    \"id\" => 1\r\r\n    \"name\" => \"abhishek singh\"\r\r\n    \"email\" => \"abhisheksingh.cotocus@gmail.com\"\r\r\n  ]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You may learn about MySQL views in this blog post and how to successfully incorporate them into Laravel apps. SQL [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How Does Laravel 9 Use MySQL View? - 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\/how-does-laravel-9-use-mysql-view\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Does Laravel 9 Use MySQL View? - DevOps Consulting\" \/>\n<meta property=\"og:description\" content=\"You may learn about MySQL views in this blog post and how to successfully incorporate them into Laravel apps. SQL [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Consulting\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-17T12:34:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-06T12:35:38+00:00\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/\",\"url\":\"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/\",\"name\":\"How Does Laravel 9 Use MySQL View? - DevOps Consulting\",\"isPartOf\":{\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#website\"},\"datePublished\":\"2023-08-17T12:34:31+00:00\",\"dateModified\":\"2023-10-06T12:35:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/\"]}]},{\"@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:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/image\/\",\"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":"How Does Laravel 9 Use MySQL View? - 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\/how-does-laravel-9-use-mysql-view\/","og_locale":"en_US","og_type":"article","og_title":"How Does Laravel 9 Use MySQL View? - DevOps Consulting","og_description":"You may learn about MySQL views in this blog post and how to successfully incorporate them into Laravel apps. SQL [&hellip;]","og_url":"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/","og_site_name":"DevOps Consulting","article_published_time":"2023-08-17T12:34:31+00:00","article_modified_time":"2023-10-06T12:35:38+00:00","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":"WebPage","@id":"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/","url":"https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/","name":"How Does Laravel 9 Use MySQL View? - DevOps Consulting","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#website"},"datePublished":"2023-08-17T12:34:31+00:00","dateModified":"2023-10-06T12:35:38+00:00","author":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsconsulting.in\/blog\/how-does-laravel-9-use-mysql-view\/"]}]},{"@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:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/image\/","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\/772","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=772"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/772\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}