Category Archives: Framework

Laravel: Get the query builder instance directly from model

Use the static query() method of abstract class Illuminate\Database\Eloquen\Model.

That’s it. Now, you can use methods like where(), find(), delete(), update(), etc.

Thanks

Laravel: Controller Dependency Injection

The Laravel service container is used to resolve all Laravel controllers. Hence, you are able to type-hint any dependencies that your controller may need in its constructor. Laravel automatically look for type hinted dependencies of controller constructor or controller method and if it knows how to create instance of dependencies it will do that and… Read More »

Laravel: Maintenance Mode

When your application is in maintenance mode, then a custom view for maintenance should be displayed for all requests into your application. To enable maintenance mode, execute the down Artisan command:

To disable maintenance mode, use the up command:

If the application is in maintenance mode, a  MaintenanceModeException will be thrown with a status code… Read More »

Laravel: Accessing Configuration Values

All of the configuration files for the Laravel framework are stored in the config directory with each configuration option properly documented. Accessing Configuration Values You can access your configuration values using the global config(‘‘, ‘‘) helper function from anywhere in your application. For Example: You can access configurations from config/app.php configuration file like below:

NOTE: After making… Read More »

Laravel: Environment configurations

Each application generally have following environments: Development Staging/Testing/UAT Production Since we have different application environments, so we may have different configurations/settings for each environment. If you install Laravel via Composer, you will find a file named .env. This environment file contains environment specific settings. So, it should be noted that your .env file should not be committed to your… Read More »

Laravel: Log Viewer

To view logs for debugging is helpful to fix issues. In Laravel, by default a single log file is generated named storage/logs/laravel.log. You can configure this to have single log file on daily basis in order to avoid a single large log file by updating ‘LOG_CHANNEL‘ environment variable value to ‘daily‘(Check .env file or configuration… Read More »

Laravel: User and Admin authentication using different tables

There are many applications where we can not treat administrators in the table where users are because users have many different properties associated with them as well as different functionalities as compared to administrators. The main key point that needs to be managed here to complete this task is have new authentication guard and user… Read More »

Laravel: Create custom helper

Laravel Custom Helper Create a folder named Helpers within app directory. Create a helper named app/Helpers/AppHelper.php

Now, register AppHelper class alias in config/app.php

That’s it. Now you can access this helper function anywhere (controller, view, etc.) in the application like below:

Thanks

Laravel: Override default registration functionality

Laravel provides some already built-in features like authentication(login) and registration that makes this framework awesome. But, sometimes we don’t want to use the default functionality or behavior provided by Laravel. Laravel default registration feature has very generic fields. But, any application’s registration requires many other fields with different type of validations. Lets consider we have fields first_name… Read More »