{"id":65754,"date":"2021-07-13T04:10:05","date_gmt":"2021-07-13T08:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/laravel-query-builder-cheatsheet-laravel\/"},"modified":"2021-07-13T04:10:05","modified_gmt":"2021-07-13T08:10:05","slug":"laravel-query-builder-cheatsheet-laravel","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=65754","title":{"rendered":"Laravel Query Builder, Cheatsheet #Laravel"},"content":{"rendered":"<p>$users = DB::table(&#8216;users&#8217;)-&gt;get()<br \/>\n$user = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;first();<br \/>\n$name = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;pluck(&#8216;name&#8217;);<br \/>\n$roles = DB::table(&#8216;roles&#8217;)-&gt;lists(&#8216;title&#8217;);<br \/>\n$roles = DB::table(&#8216;roles&#8217;)-&gt;lists(&#8216;title&#8217;, &#8216;name&#8217;);<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name&#8217;, &#8217;email&#8217;)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;distinct()-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name as user_name&#8217;)-&gt;get();<br \/>\n$query = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name&#8217;);<br \/>\n$users = $query-&gt;addSelect(&#8216;age&#8217;)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;orWhere(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;whereBetween(&#8216;votes&#8217;, array(1, 100))-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;whereNotBetween(&#8216;votes&#8217;, array(1, 100))-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;whereIn(&#8216;id&#8217;, array(1, 2, 3))-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;whereNotIn(&#8216;id&#8217;, array(1, 2, 3))-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;whereNull(&#8216;updated_at&#8217;)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;orderBy(&#8216;name&#8217;, &#8216;desc&#8217;)-&gt;groupBy(&#8216;count&#8217;)-&gt;having(&#8216;count&#8217;, &#8216;&gt;&#8217;, 100)-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;skip(10)-&gt;take(5)-&gt;get();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;join(&#8216;contacts&#8217;, &#8216;users.id&#8217;, &#8216;=&#8217;, &#8216;contacts.user_id&#8217;)<br \/>\n-&gt;join(&#8216;orders&#8217;, &#8216;users.id&#8217;, &#8216;=&#8217;, &#8216;orders.user_id&#8217;)<br \/>\n-&gt;select(&#8216;users.id&#8217;, &#8216;contacts.phone&#8217;, &#8216;orders.price&#8217;)<br \/>\n-&gt;get();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;leftJoin(&#8216;posts&#8217;, &#8216;users.id&#8217;, &#8216;=&#8217;, &#8216;posts.user_id&#8217;)-&gt;get();<br \/>\nDB::table(&#8216;users&#8217;)<br \/>\n-&gt;join(&#8216;contacts&#8217;, function($join)<br \/>\n{<br \/>\n$join-&gt;on(&#8216;users.id&#8217;, &#8216;=&#8217;, &#8216;contacts.user_id&#8217;)-&gt;orOn(&#8230;);<br \/>\n})<br \/>\n-&gt;get();<\/p>\n<p>DB::table(&#8216;users&#8217;)<br \/>\n-&gt;join(&#8216;contacts&#8217;, function($join)<br \/>\n{<br \/>\n$join-&gt;on(&#8216;users.id&#8217;, &#8216;=&#8217;, &#8216;contacts.user_id&#8217;)<br \/>\n-&gt;where(&#8216;contacts.user_id&#8217;, &#8216;&gt;&#8217;, 5);<br \/>\n})<br \/>\n-&gt;get();<\/p>\n<p>select * from users where name = &#8216;John&#8217; or (votes &gt; 100 and title &lt;&gt; &#8216;Admin&#8217;)<br \/>\nDB::table(&#8216;users&#8217;)<br \/>\n-&gt;where(&#8216;name&#8217;, &#8216;=&#8217;, &#8216;John&#8217;)<br \/>\n-&gt;orWhere(function($query)<br \/>\n{<br \/>\n$query-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)<br \/>\n-&gt;where(&#8216;title&#8217;, &#8216;&lt;&gt;&#8217;, &#8216;Admin&#8217;);<br \/>\n})<br \/>\n-&gt;get();<\/p>\n<p>select * from users<br \/>\nwhere exists (<br \/>\nselect 1 from orders where orders.user_id = users.id<br \/>\n)<br \/>\nDB::table(&#8216;users&#8217;)<br \/>\n-&gt;whereExists(function($query)<br \/>\n{<br \/>\n$query-&gt;select(DB::raw(1))<br \/>\n-&gt;from(&#8216;orders&#8217;)<br \/>\n-&gt;whereRaw(&#8216;orders.user_id = users.id&#8217;);<br \/>\n})<br \/>\n-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)-&gt;count();<br \/>\n$price = DB::table(&#8216;orders&#8217;)-&gt;max(&#8216;price&#8217;);<br \/>\n$price = DB::table(&#8216;orders&#8217;)-&gt;min(&#8216;price&#8217;);<br \/>\n$price = DB::table(&#8216;orders&#8217;)-&gt;avg(&#8216;price&#8217;);<br \/>\n$total = DB::table(&#8216;users&#8217;)-&gt;sum(&#8216;votes&#8217;);<\/p>\n<p>$first = DB::table(&#8216;users&#8217;)-&gt;whereNull(&#8216;first_name&#8217;);<\/p>\n<p>$users = DB::table(&#8216;users&#8217;)-&gt;whereNull(&#8216;last_name&#8217;)-&gt;union($first)-&gt;get();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;lockForUpdate()-&gt;get();<br \/>\n$users = DB::table(&#8216;users&#8217;)<br \/>\n-&gt;select(DB::raw(&#8216;count(*) as user_count, status&#8217;))<br \/>\n-&gt;where(&#8216;status&#8217;, &#8216;&lt;&gt;&#8217;, 1)<br \/>\n-&gt;groupBy(&#8216;status&#8217;)<br \/>\n-&gt;get();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;insert(<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;john@example.com&#8217;, &#8216;votes&#8217; =&gt; 0)<br \/>\n);<br \/>\n$id = DB::table(&#8216;users&#8217;)-&gt;insertGetId(<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;john@example.com&#8217;, &#8216;votes&#8217; =&gt; 0)<br \/>\n);<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;insert(array(<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;taylor@example.com&#8217;, &#8216;votes&#8217; =&gt; 0),<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;dayle@example.com&#8217;, &#8216;votes&#8217; =&gt; 0),<br \/>\n));<br \/>\nDB::table(&#8216;users&#8217;)-&gt;insert(array(<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;taylor@example.com&#8217;, &#8216;votes&#8217; =&gt; 0),<br \/>\narray(&#8217;email&#8217; =&gt; &#8216;dayle@example.com&#8217;, &#8216;votes&#8217; =&gt; 0),<br \/>\n));<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;increment(&#8216;votes&#8217;);<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;increment(&#8216;votes&#8217;, 5);<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;decrement(&#8216;votes&#8217;);<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;decrement(&#8216;votes&#8217;, 5);<br \/>\nDB::table(&#8216;users&#8217;)-&gt;increment(&#8216;votes&#8217;, 1, array(&#8216;name&#8217; =&gt; &#8216;John&#8217;));<br \/>\nDB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&lt;&#8216;, 100)-&gt;delete();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;delete();<br \/>\nDB::table(&#8216;users&#8217;)-&gt;truncate();<\/p>\n<p>$first = DB::table(&#8216;users&#8217;)-&gt;whereNull(&#8216;first_name&#8217;);<\/p>\n<p>$users = DB::table(&#8216;users&#8217;)-&gt;whereNull(&#8216;last_name&#8217;)-&gt;union($first)-&gt;get();<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;sharedLock()-&gt;get();<\/p>\n<p>DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;lockForUpdate()-&gt;get();<\/p>\n<p>$users = DB::table(&#8216;users&#8217;)-&gt;remember(10)-&gt;get();<\/p>\n<p>$users = DB::table(&#8216;users&#8217;)-&gt;cacheTags(array(&#8216;people&#8217;, &#8216;authors&#8217;))-&gt;remember(10)-&gt;get();<\/p>\n<p>Reference: Laravel online Documentation From: http:\/\/sitestree.com\/?p=4666<br \/> Categories:Laravel<br \/>Tags:<br \/> Post Data:2016-12-07 21:38:15<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>$users = DB::table(&#8216;users&#8217;)-&gt;get() $user = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;first(); $name = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;pluck(&#8216;name&#8217;); $roles = DB::table(&#8216;roles&#8217;)-&gt;lists(&#8216;title&#8217;); $roles = DB::table(&#8216;roles&#8217;)-&gt;lists(&#8216;title&#8217;, &#8216;name&#8217;); $users = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name&#8217;, &#8217;email&#8217;)-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;distinct()-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name as user_name&#8217;)-&gt;get(); $query = DB::table(&#8216;users&#8217;)-&gt;select(&#8216;name&#8217;); $users = $query-&gt;addSelect(&#8216;age&#8217;)-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;where(&#8216;votes&#8217;, &#8216;&gt;&#8217;, 100)-&gt;orWhere(&#8216;name&#8217;, &#8216;John&#8217;)-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;whereBetween(&#8216;votes&#8217;, array(1, 100))-&gt;get(); $users = DB::table(&#8216;users&#8217;)-&gt;whereNotBetween(&#8216;votes&#8217;, &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=65754\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-65754","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":69093,"url":"http:\/\/bangla.sitestree.com\/?p=69093","url_meta":{"origin":65754,"position":0},"title":"Securing MySQL Database #5","author":"Author-Check- Article-or-Video","date":"August 12, 2021","format":false,"excerpt":"After installing mysql, we will need to remove test database and associated users and their permissions. We will need to use mysql database to remove associated users and permission.--DROP DATABASE test; --SELECT db.Host, db.Db, db.User, db.Select_priv -> FROM db WHERE (db.DB = \"vworksDB\");---SELECT db.User, db.Host, db.Db -> FROM db ->\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":20797,"url":"http:\/\/bangla.sitestree.com\/?p=20797","url_meta":{"origin":65754,"position":1},"title":"DB Operations for Laravel (a MVC based PHP framework). The Model is also discussed","author":"Author-Check- Article-or-Video","date":"February 27, 2021","format":false,"excerpt":"Different methods of DB operations are discussed. How to model using the Eloquent ORM is also discussed. DB operations are shown in Plain SQL, or Using the Query builder, and also using the Model. In a later document, I will show the details of Laravel configurations and some MVC coding\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24062,"url":"http:\/\/bangla.sitestree.com\/?p=24062","url_meta":{"origin":65754,"position":2},"title":"#LARAVEL. Misc Laravel News and Tutorials: PHP&#8217;s Git Server Compromised, Laravel Lambo, HasManyThrough Relations hips, and more &#8211; \u2116353","author":"Author-Check- Article-or-Video","date":"April 4, 2021","format":false,"excerpt":"PHP's Git Server Compromised HasManyThrough Relationships With Unlimited Levels Manage PHP and Valet from the macOS Status Bar Lambo CLI for Quick Application Creation With Laravel and Valet Major Heroicons Release Includes Vue\/React Components and Figma Files Soap - A Laravel SOAP client that provides a clean interface for handling\u2026","rel":"","context":"In &quot;Laravel&quot;","block_context":{"text":"Laravel","link":"http:\/\/bangla.sitestree.com\/?cat=1852"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7731,"url":"http:\/\/bangla.sitestree.com\/?p=7731","url_meta":{"origin":65754,"position":3},"title":"MySQL \u098f\u09b0 \u09a1\u09be\u099f\u09be\u09ac\u09c7\u099c \u0995\u09c7 \u09b8\u09c1\u09b0\u0995\u09cd\u09b7\u09bf\u09a4 \u0995\u09b0\u09be","author":"Author-Check- Article-or-Video","date":"March 28, 2015","format":false,"excerpt":"MySQL \u098f\u09b0 \u09a1\u09be\u099f\u09be\u09ac\u09c7\u099c \u0995\u09c7 \u09b8\u09c1\u09b0\u0995\u09cd\u09b7\u09bf\u09a4 \u0995\u09b0\u09be ------------------------------------------------- MySQL \u0987\u09a8\u09cd\u09b8\u099f\u09b2\u09c7\u09b6\u09a8\u09c7\u09b0 \u09aa\u09b0 \u0986\u09ae\u09be\u09a6\u09c7\u09b0 \u099f\u09c7\u09b8\u09cd\u099f \u09a1\u09be\u099f\u09be\u09ac\u09c7\u099c, \u09b8\u0982\u09b6\u09cd\u09b2\u09bf\u09b7\u09cd\u099f \u0987\u0989\u099c\u09be\u09b0 \u098f\u09ac\u0982 \u09a4\u09be\u09a6\u09c7\u09b0 \u09aa\u09be\u09b0\u09ae\u09bf\u09b6\u09a8 \u0985\u09aa\u09b8\u09be\u09b0\u09a8 \u0995\u09b0\u09a4\u09c7 \u09b9\u09ac\u09c7 \u0964 \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf\u099f\u09bf \u09a8\u09bf\u099a\u09c7 \u09ac\u09cd\u09af\u0996\u09cd\u09af\u09be \u0995\u09b0\u09be \u09b9\u09b2\u0983 -- DROP DATABASE test; -- SELECT db.Host, db.Db, db.User, db.Select_priv -> FROM db WHERE (db.DB = \"vworksDB\"); --- SELECT db.User, db.Host, db.Db ->\u2026","rel":"","context":"In &quot;\u09ae\u09be\u0987 \u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 \/MYSQL&quot;","block_context":{"text":"\u09ae\u09be\u0987 \u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 \/MYSQL","link":"http:\/\/bangla.sitestree.com\/?cat=257"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23198,"url":"http:\/\/bangla.sitestree.com\/?p=23198","url_meta":{"origin":65754,"position":4},"title":"Securing MySQL Database #Root #By Sayed Ahmed","author":"Author-Check- Article-or-Video","date":"March 26, 2021","format":false,"excerpt":"Mainly deals with TestDb After installing mysql, we will need to remove test database and associated users and their permissions. We will need to use mysql database to remove associated users and permission. -- DROP DATABASE test; -- SELECT db.Host, db.Db, db.User, db.Select_priv -> FROM db WHERE (db.DB = \"vworksDB\");\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":70211,"url":"http:\/\/bangla.sitestree.com\/?p=70211","url_meta":{"origin":65754,"position":5},"title":"IBM DB 2: free version of IBM DB2 #11","author":"Author-Check- Article-or-Video","date":"August 29, 2021","format":false,"excerpt":"DB2 Express-C is the free edition of DB2 for Linux and Windows. It is free to download, develop applications, deploy into production, and even redistribute. DB2 Express-C supports development of database applications using XML, C\/C++, .NET, JDBC, ODBC, PHP, Ruby, and more. It is optimized for systems with up to\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/65754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=65754"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/65754\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=65754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=65754"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=65754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}