{"id":11669,"date":"2022-02-15T10:00:00","date_gmt":"2022-02-15T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11669"},"modified":"2022-02-14T19:50:46","modified_gmt":"2022-02-14T10:50:46","slug":"some-laravel-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11669","title":{"rendered":"Some Laravel Tips and Tricks"},"content":{"rendered":"\n<p><font>Today I would like to share some laravel tips an<\/font>d tricks. Let&#8217;s take a look.<\/p>\n\n\n\n<h3><strong>Use Carbon with Only Hours<\/strong><\/h3>\n\n\n\n<p>If you want to have a current date without seconds and\/or minutes, use Carbon&#8217;s methods like setSeconds(0) or setMinutes(0).<\/p>\n\n\n\n<p>echo now();<\/p>\n\n\n\n<p>\/\/ 2022-02-14 02:12:34<\/p>\n\n\n\n<p>echo now()-&gt;setSeconds(0);<\/p>\n\n\n\n<p>\/\/ 2022-02-14 02:12:00<\/p>\n\n\n\n<p>echo now()-&gt;setSeconds(0)-&gt;setMinutes(0);<\/p>\n\n\n\n<p>\/\/ 2022-02-14 &nbsp;02:00:00<\/p>\n\n\n\n<p>\/\/ Another way &#8211; even shorter<\/p>\n\n\n\n<p>echo now()-&gt;startOfHour();<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Generate Images with Seeds\/Factories<\/strong><\/h3>\n\n\n\n<p>Normally, We use Faker to generate fake text values in seeds and factories. But do you know that you can generate not only text values but also Images by Faker?<\/p>\n\n\n\n<p>For example: See image field here &#8211; it will generate 100&#215;100 image:<\/p>\n\n\n\n<p>$factory-&gt;define(User::class, function (Faker $faker) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; return [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;name&#8217; =&gt; $faker-&gt;name,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;email&#8217; =&gt; $faker-&gt;unique()-&gt;safeEmail,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;email_verified_at&#8217; =&gt; now(),<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;password&#8217; =&gt; bcrypt(&#8216;password&#8217;),<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;remember_token&#8217; =&gt; Str::random(10),<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;Image&#8217; =&gt; $faker-&gt;image(storage_path(&#8216;images&#8217;), 100, 100)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; ];<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Simple Pagination<\/strong><\/h3>\n\n\n\n<p>In pagination, if you need just &#8220;Previous\/next&#8221; links instead of all the page numbers (and have fewer DB queries because of that), You can do this easily by changing paginate() to simplePaginate():<\/p>\n\n\n\n<p>\/\/ Instead of<\/p>\n\n\n\n<p>$products = Product::paginate(8);<\/p>\n\n\n\n<p>\/\/ Now you can do this<\/p>\n\n\n\n<p>$products = Product::simplePaginate(8);<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Use @auth in Blade<\/strong><\/h3>\n\n\n\n<p>Instead of an if-statement to check logged-in users, You can use the @auth directive in your blade file.<\/p>\n\n\n\n<p>Old way:<\/p>\n\n\n\n<p>@if(auth()-&gt;user())<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ The user is authenticated.<\/p>\n\n\n\n<p>@endif<\/p>\n\n\n\n<p>Latest:<\/p>\n\n\n\n<p>@auth<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ The user is authenticated.<\/p>\n\n\n\n<p>@endauth<\/p>\n\n\n\n<p>The opposite is @guest directive:<\/p>\n\n\n\n<p>@guest<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ The user is not authenticated.<\/p>\n\n\n\n<p>@endguest<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Check the view file exists<\/strong><\/h3>\n\n\n\n<p>You can check if the View file exists before actually loading it.<\/p>\n\n\n\n<p>if (view()-&gt;exists(&#8216;admin.dashboard&#8217;)) {<\/p>\n\n\n\n<p>&nbsp;\/\/ Load the view<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>You can even load an array of views, and only the first existing will be loaded.<\/p>\n\n\n\n<p>return view()-&gt;first([&#8216;admin.dashboard&#8217;, &#8216;dashboard&#8217;], $data);<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Create migration without underscore _ symbol<\/strong><\/h3>\n\n\n\n<p>When you are running a make:migration command, you don&#8217;t necessarily have to write underscore _ symbol between parts, like create_users_table.<\/p>\n\n\n\n<p>\/\/ with underscore _ symbol<\/p>\n\n\n\n<p>php artisan make:migration create_users_table<\/p>\n\n\n\n<p>You can enter the name into quotes and then utilize spaces instead of underscores.<\/p>\n\n\n\n<p>\/\/ without underscore _ symbol<\/p>\n\n\n\n<p>php artisan make:migration &#8220;create users table&#8221;<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Use Auth::once()<\/strong><\/h3>\n\n\n\n<p>Do you know that you c an log in with the user only for One Request? You can easily do this by utilizing the method Auth::once(). No sessions or cookies will be used, which indicates this method may be applicable when building a stateless API.<\/p>\n\n\n\n<p>if (Auth::once($credentials)) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><font><\/p>\n\n\n\n<h3><strong>Use @canany to check multiple permissions at once<\/strong><\/h3>\n\n\n\n<p>Earlier, You use the @can directive to check if a user has a certain permission. Now, you can also check multiple permissions at once with the @canany directive.<\/p>\n\n\n\n<p>@canany([&#8216;update&#8217;, &#8216;view&#8217;, &#8216;delete&#8217;], $blog)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ The current user can update, view, or delete the blog post<\/p>\n\n\n\n<p>@elsecanany([&#8216;create&#8217;], \\App\\Blog::class)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ The current user can create a blog post<\/p>\n\n\n\n<p>@endcanany<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This is all for now. Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11669\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11669\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Some Laravel Tips and Tricks\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11669\" data-text=\"Some Laravel Tips and Tricks\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11669\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11669\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to share some laravel tips and tricks. Let&#8217;s take a look. Use Carbon with Only Hours  [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11669"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11669"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11669\/revisions"}],"predecessor-version":[{"id":11672,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11669\/revisions\/11672"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}