Best code snippets, tutorials and more

Coursecomp is your starting point for developing and maintaining your web application.

How to register long query callback in Laravel

If you are running your Laravel app in production mode, you may want to register long running queries, which is very useful for debugging purposes: You can also sort these long queries by their duration of execution, so that the longest running ones are easier to see: Via @realstevebauman.

How to measure the performance with dd() in Laravel

We now have quite a good option with the new implementation of dd() method. So, since Laravel 9.32.0 you can easily test the performance of any part of your Laravel application. Just add new “Benchmark” class and you will be able the time it takes (in milliseconds) for the certain callbacks to complete: You can […]

How to add hash (#) to a route redirect in Laravel

If you want to use redirect to a specific section on a page, you might want to use withFragment(), so you can use the fragment of the URI: You can also utilize this method when redirecting upon form completion: Via @ecrmnn

How to make code shorter with QueryBuilder in Laravel

Imagine, we need to get the name of the first admin in our web application. We can do that in two ways: The first way, using four lines of code, and if the user is not present, it will throw an error: The second way uses only 2 lines of code, and if the user […]

How to use URL Facade in Laravel

Laravel quick tip! URL Facade is a cool feature in Laravel that allows you to extract the current, previous, and full URLs on any given web page. See the example below: https://coursecomp.com/user/admin/ https://coursecomp.com/user/admin/?foo=bar https://coursecomp.com/news/

How to check if PIP is installed or not on Mac OS

Python is a programming language with many features that make it well suited for scripting, automation, and data analysis. The latest version of python for Mac OS is 3.6.3. This version includes bug fixes and improvements to existing features, with the most notable change of adding an enclosure keyword and support for type annotation. You […]

How to use count() within a loop in PHP? Put it outside

In order to make your code cleaner, the best way is to use the count() function outside of the loop. See the example below: What it count() in PHP? The count() function is a built-in function in PHP that we use to count the number of elements in an array. We can also use it […]

How to update timestamp column in Laravel

If you want to update a timestamp column in a specific model, you can use touch method, instead of using update. This functionality has been added Since Laravel 9.25 it is now also possible to use the touch method on the query builder as well (was only possible on a model previously). But remember, touch […]

How to display 0 but not empty string values or null in Laravel

There is a beautiful filled() method in Laravel so that you can avoid unnecessary outputs in your rendering blade-templates. Here is the example: Quantity per serving Energy 1438 kJ / 338 kcal Protein 0 Fat 0 Carbohydrate 85 See the official documentation. Thnx to Daniel Eckermann.