Best code snippets, tutorials and more

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

How to check if a string is valid JSON in Laravel

It is very common when you have to check whether a string is a valid JSON or not. An example of Str::isJson in Laravel In Laravel, you may want to use Str::isJson method as follows: These are examples from the official documentation: What is JSON for? JSON is a data format that helps exchange data […]

How to use constructor promotion in PHP 8

This is so good! The only thing that feels wrong is the empty brackets. What is a constructor property promotion? It is a new syntax that PHP 8 brings, allowing two simultaneous operations: Class property declaration and Constructor assignment, right from the constructor. See how it works: See the official documentation.

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:

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 […]