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 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.

How to send email with uploaded files attached In Laravel

Laravel tip: if you want to send emails having user uploaded files attached, you don’t need to save the file first. Instead, you just attach the uploaded file with attachData method and that’s it! Here is the example from a Mailable class: Via @ecrmnn.

How to use whereColumn() method in Laravel

Quick tip: you can use a pretty handy Eloquent method called whereColumn() to verify if columns are equal. For example, you can find posts that have been updated or posts which have the same title and slug. Here is the code: Read more in the official documentation here.

How to reduce code duplication with relationships in Laravel

Since Laravel 9.16 we can reduce the code a bit, using withWhereHas() method. It’s time to do some code clean up! See the example below: As Laravel contributor Eusonlito describes: There are a lot of times that we need to load a relationship using with with the same conditions that we use for whereHas. This method simplifies this […]