tag:blogger.com,1999:blog-25815215721115855042024-03-12T21:08:32.323-07:00Omar's BlogOmar Iqbal Naruhttp://www.blogger.com/profile/09609214589291922309noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2581521572111585504.post-65831551865894521692021-04-29T14:48:00.001-07:002021-04-29T14:48:50.553-07:00Laravel 8 Upgrade Strategy<div>Recently me and my team has been working on Laravel 8 upgrade from Laravel 7.</div><div><br /></div><div>To facilitate the upgrade we decided to use <a href="ttps://laravelshift.com/" target="_blank">Laravel Shift</a>.</div><div><br /></div><div>After reviewing the initial PR created by Laravel Shift, we decided that it would be too risky to make all the changes done by Laravel Shift at once. </div><div><br /></div><div>Luckily Laravel Shift also provides a Laravel Workbench if you get the yearly subscription for 99 USD/yr for latest version upgrade. By latest version they mean that as long as Laravel 8 is available, upgrades from Laravel 7 to Laravel 8 would be covered under this subscription. Once Laravel 9 is release, upgrades from Laravel 8 to Laravel 9 will be convered also under this subscription. </div><div><br /></div><div>Laravel Workbench can be used to run different tasks e.g. Upgrade to Laravel Mix 6, Class Based Routes etc.</div><div><br /></div><div>So we decided to go with the following approach:</div><div><br /></div><div>- Upgrade Laravel Mix to 6</div><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><div style="text-align: left;">This went pretty smoothly.</div></blockquote><div><br /></div><div>- Upgrade Routes to Class Based Routes</div><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><div style="text-align: left;">For resource based controllers we ran into a problem in which test case was still picking up namespace from somewhere and appending it in the start. In Laravel 8 the default namespace defined in RouteServiceProvider.php has been removed so in web.php and api.php you have to use fully qualified class names e.g.</div></blockquote><div><br /></div><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><div>Route::get('/users', [\App\Http\Controllers\UserController::class, 'index']);</div><div><br /></div><div>For resource controller you would expect this to work:</div><div><br /></div><div>Route::resource('/users', \App\Http\Controllers\UserController::class);</div><div><br /></div><div>However when working testcase somehow it tries to get the UserController class from the following path:</div><div><br /></div><div>Route::resource('/users', \App\Http\Controllers\App\Http\Controllers\UserController::class);</div><div><br /></div><div>To fix it, after trying multiple things we changed the definition as following to finally get it to work</div><div><br /></div><div>Route::resource('/users', '\App\Http\Controllers\UserController');</div></blockquote><div><br /></div><div>- Formatting</div><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><div style="text-align: left;">We upgraded our .php_cs.dist file and ran php-cs-fixer locally first on the whole project using the <a href="https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200" target="_blank">recommendation</a> by Laravel Shifter. Then ran laravel formatter task of Laravel Shift workbench to finalise changes.</div></blockquote><div><br /></div><div>- Final Upgrade including moving to Model Factories, Version Upgrades etc.</div><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><div style="text-align: left;">When we ran composer install there were a lot of packages not compatible with Laravel 8 and PHP 8. So we had to upgrade their versions that were compatible one by one of replace them. This will vary depending upon the packages you use in your project.</div></blockquote><div><br /></div><div>Hopefully this will help as a starting point for your Laravel 8 Upgrades. Please go through the following <a href="https://laravel.com/docs/8.x/upgrade" target="_blank">link</a> to see the official documentation regarding the upgrade.</div>Omar Iqbal Naruhttp://www.blogger.com/profile/09609214589291922309noreply@blogger.com0tag:blogger.com,1999:blog-2581521572111585504.post-84130121714255404222021-04-24T18:34:00.003-07:002021-04-24T18:34:42.031-07:00Useful Tools for Monitoring Your Applications<p>During the last year or so me and my team have worked a lot in setting up monitoring and alerts for our applications in production so that we can work on fixing the issues proactively rather than react to issues reported by our end clients.</p><p>We are so far using the following tools:</p><p></p><ul style="text-align: left;"><li><a href="http://newrelic.com">Newrelic</a></li><ul><li>Infrastructure Alerts - We have setup infrastructure alerts to report any issues related to high cpu usage, high memory usage, low disk space and host not reporting.</li><li>APM</li><li>Logging</li><li>Cost - Its FREE to use for up to 100GB / month which is a fantastic offer to evaluate the toolset provided by Newrelic.</li></ul><li><a href="http://healthchecks.io">healthchecks.io</a></li><ul><li>Using it to monitor status of our crons</li></ul><li><a href="https://uptimerobot.com/?gclid=CjwKCAjwg4-EBhBwEiwAzYAlskl6FuNbKdhZXmH96qT1puor0mBx0K1DvdA4T22GXjz2A_qA551UFRoCcCMQAvD_BwE">UptimeRobot</a></li><ul><li>Using it to monitor status of our websites.</li></ul></ul><div>All of these tools integrate with Slack which is a big plus.</div><div><br /></div><div>In case you guys need some more information regarding our setup, please feel free to send me a message on LinkedIn @ https://www.linkedin.com/in/omariqbalnaru/</div><p></p>Omar Iqbal Naruhttp://www.blogger.com/profile/09609214589291922309noreply@blogger.com0tag:blogger.com,1999:blog-2581521572111585504.post-16085430097575360782021-04-24T18:14:00.004-07:002021-04-24T18:23:42.377-07:00Laravel Tip: Saving/Updating Eloquent model with associated entities with single line of code<p>Imagine you have two entities Employees and Department.</p><p>Usually if you want to update something in department and its associated employee you will do something like this:</p><script src="https://gist.github.com/omariqbalnaru/8a2edff546d8ae361ed5a72e2de7c6e4.js"></script><p><br /></p><p>However you save all associated relationships in a single line with just:</p><p>$department->push();</p><p><br /></p><p>Source: <a href="https://laravel.com/docs/8.x/eloquent-relationships#the-push-method">https://laravel.com/docs/8.x/eloquent-relationships#the-push-method</a></p>Omar Iqbal Naruhttp://www.blogger.com/profile/09609214589291922309noreply@blogger.com0