7 min read

Laravel FAQs: Quick Answers To Common Queries – Pt 4

Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.

laravel faq laravel policy

We are back with our Laravel FAQ session! This time, we’ve prepared fresh answers to the commonly asked queries around the Laravel ecosystem. From creating custom artisan commands to defining Laravel Eloquent wherein and Laravel Policy, this article will guide you through the Laravel essentials and help you make your development process even smoother.

Laravel Eloquent wherein

How to Check Laravel Version in Terminal?

To check your Laravel version using the terminal, simply run the following command:

php artisan --version

This command will display the Laravel version installed in your project. It’s a quick and easy way to make sure you’re working with the right version, especially when updating or troubleshooting.

You can also check composer.json file: Open the composer.json file in your project directory and search for the “laravel/framework” entry. The version number will be listed next to it.

How To Create Custom Artisan Commands in Laravel?

Creating custom Artisan commands in Laravel involves a few steps:

  1.  Generate the Command: Use the Artisan CLI to generate a new command template:
php artisan make:command YourCommandName
  1. Configure the Command: Edit the generated command file located in
  1.  Set the command signature (your:command) and description in the $signature and $description properties.

  2. Implementing Logic: Write your command’s logic in the handle() method of the command class.
  3. Registering the Command: Finally, register your new command in the commands array of the app/Console/Kernel.php file.
  4. Execute Command: Run your new command using php artisan your:command.

Here is a basic example. You can enhance your command by adding more arguments and options and implementing more complex logic within the handle method.

namespace App\Console\Commands;
use Illuminate\Console\Command;
class GreetCommand extends Command
    // The name and signature of the console command.
    protected $signature = 'greet:name {name}';
    // The console command description.
    protected $description = 'Greet someone by name';
    // Execute the console command.
    public function handle()
        $name = $this->argument('name');
        $this->info("Hello, $name!");
// php artisan greet:name John 
// Will output: Hello, John!

How Does Laravel Task Scheduling Work?

Laravel’s task scheduling allows you to define your command schedule fluently and expressively within Laravel itself without having to create a separate cron entry for each task. Schedule your tasks using the schedule method of the app/Console/Kernel.php file. 

You need to add a single cron entry on your server that runs the php artisan schedule:run the command every minute:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

What is a Service Provider in Laravel?

Service providers in Laravel have a central place for all application configurations. They bootstrap your application by binding services in the service container, registering events, or performing other tasks to prepare your application for incoming requests.

  • Registration and Bootstrapping: Service providers contain two important methods: register() and boot(). The register() method is used for binding services to the container, while boot() is used for bootstrapping any application services.
  • Creating Service Providers: You can create your own service providers using the php artisan make:provider command. This is useful for organizing application initialization logic.

What is Laravel Eloquent wherein and How to Use It?

Laravel Eloquent wherein method allows you to query against a set of values. For example, if you want to retrieve users with specific IDs, you can use

User::whereIn('id', [1, 2, 3])->get();

This method is especially useful when you need to filter results based on a range of values in a column.

What is Laravel Sanctum, and How Does It Help with API Authentication?

Laravel Sanctum provides a simple, lightweight system for API token authentication. It’s particularly suited for single-page applications (SPAs), mobile applications, and simple token-based APIs.

  • Token Creation: Sanctum allows each user of your application to generate multiple API tokens for their account. These tokens can be granted abilities/scopes which specify which actions the tokens are allowed to perform.
  • Stateful vs. Stateless: Sanctum can authenticate your SPA with a “stateful” cookie-based session or authenticate your API tokens in a “stateless” manner, offering flexibility based on your application’s needs.

How to Use Laravel Eloquent Relationships Like One-to-One, One-to-Many, and Many-to-Many?

Laravel Eloquent supports defining various relationships between your models.

  • One-to-one: Use the hasOne or belongsTo methods to represent a one-to-one relationship between models: A direct relationship between two models. For example, each user might have one profile.
  • One-to-many: Use the hasMany or belongsTo methods depending on the direction of the relationship. It is when a single model owns multiple models. For instance, a blog post (hasMany) may have many comments (belongsTo).
  • Many-to-many: Use the belongsToMany method to define a many-to-many relationship between models. A relationship where models can belong to multiple other models. For example, a user can belong to multiple teams, and each team can have multiple users.

Each method provides additional methods to access and manage related models and data.

What is Laravel Policy and Laravel Gate, and How Do They Assist with Authorization?

Laravel Gates and Policies are used for authorization purposes. They help you determine if a user can perform a given action against a resource.

  • Larave Gate: Gates are closures that determine if a user can perform a particular action. They are defined in the AuthServiceProvider and are useful for simple authorization rules.
  • Laravel Policy: Policies are classes that organize authorization logic around a particular model or resource. For example, a PostPolicy might determine if a user can update a blog post. They are registered in the AuthServiceProvider.

Using Gates and Policies, you can centralize your authorization logic and keep controllers clean and focused on serving HTTP requests.

Laravel Policy gives you the ability to check permissions via the User model, using can and cannot methods:

if ($request->user()->cannot('update', $post)) {

Meet the authors

We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.

Contact us