Best code snippets, tutorials and more

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

Quick tip on how to use cache in Laravel

You can easily cache a database query or any other data in the HTTP requests with Laravel’s Cache facade. The closure function passed to the remember method will be used if the cache doesn’t exist. See the example below:

How to make Mailgun integration work after upgrading to Laravel 9

If you’re receiving errors like "Class Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory not found", you need to know that the app codebase has been impacted by one of the largest changes in Laravel 9, when the development team transitioned from SwiftMailer to Symfony Mailer. Let’s fix the problem. Run this command in your Laravel application folder: Also, don’t forget to […]

How to get records between two timestamps in Laravel

If you need to retrieve records between two timestamps, you can use whereBetween method with a from and to date. You can also to provide a fallback with the null coalescing operator (which was implemented in PHP since version 7+). by @ecrmnn

How to add changes to Model and its relationships in Laravel

When making changes to a Model and its relations in Laravel, you don’t need to call save() method on each of them. Instead, you just can call push() on the main Model and it will recursively save any changes. by @mattkingshott

How to add Github Gist to a WordPress post

In order to show your Github Gist, you need to add a Custom HTML block in your WordPress editor, then paste the javascript embed link that you’ve got from Github. Your WordPress editor area: You can also make sure that everything is OK by switching to Preview option: Enjoy!

How to get only specific columns in Eloquent using relationships “with()”

If you have relationships like hasMany or manyToMany in Laravel and you need to extract data from specific columns of your DB table with your Eloquent query, just do it like that: Don’t forget to always use a column with primary key (in the example above it is id column) and not to put spaces […]

How to change default validation messages in Laravel

Sometimes you need to change the default text of validation error messages returned when data validation fails. Fortunately, with form requests, you can make our own error messages to make it clearer to the user.