Best code snippets, tutorials and more

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

How to make queries to large tables in Laravel

You should avoid using SQL functions in your Laravel Eloquent queries because they inflict full table scans. This happens due to database mechanisms – the condition is not applied until the date function has been run. It will produce a query like this: The better way: So we get this exact query, without the date() […]

How to delete a column from table in Laravel

To remove a column from a database table in Laravel, you need to create a migration, edit, and then run it in the command line interface. Create the migration You need to open your Laravel application folder in Terminal. Then run one of the following commands. Remember that you can skip typing underscore symbols, see […]

How to validate a database column on uniqueness in Laravel

You can see if the column in your Laravel application DB is unique or not. For example, the article table has slug and author_id with the unique slug for each author. See the code below, it’s useful for multi-tenancy:

How to parse Carbon dates with useful macro in Laravel

When you try to parse a date with Carbon, by default it will throw an exception when parsing an invalid date format. So you need to manually try/catch the exception in your code. But sometimes, you might need a false value rather than dealing with the exception. So here is the macro that will help […]

How to use conditions in Eloquent relationships

You can use conditions in Eloquent with relationships. For example, we’ve got a Team and its Users. We can define the relationship with the name “admins” which extends the user relation with a condition.

How to edit Nginx configuration in Laravel Forge

There are basically two options to manage Nginx configuration with Laravel Fore. 1. Create custom Nginx template before adding a site In the Servers section of the Forge panel, you should select Nginx Templates menu option, then add your custom name to the configuration file. Then, change the template file according to your needs and […]