Laravel Query Scope

In Laravel most of the time you want to get a conditional result by using where function, so if I have to append many where on many places in my code best way can be “Query scope”

Check this example

$active_booked = User::where('active', '=', 1)->where('is_booked', '=', 1)->get();

So in your model create query scope methods to short this scenario

<?php

  namespace App;

  class User extends Model
  {
    public function scopeActive($query)
    {
        return $query->where('active', '=', 1);
    }

    public function scopeBooked($query)
    {
        return $query->where('is_booked', '=', 1);
    }
  }
?>

In your controller call simply like this

$active_booked = User::active()->booked()->get();

Laravel Console – Laravel custom command

Artisan Console

Artisan is Laravel command line interface, which has lot of per-build commands, to see this list just type

php artisan list

at top you can see options which you can use with any command like –help option with any command like

php artisan migrate  --help

Laravel Custom Command Line

In addition to pre-build command Laravel has option to create your own commands, for this you have to run this command and the respective file will be created in “App\Console\Commands” directory

php artisan make:command HiCommand

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class HiCommand extends Command
{

    protected $signature = 'command:name';
    protected $description = 'Command description';

    public function __construct()
    {
        parent::__construct();
    }
    
    public function handle()
    {
        return 0;
    }
}

So there are few main things

  1. Signature:- command:name let me we change this with say:hi
  2. Description:- Let change this to “This is my test command.”
  3. Construct Function
  4. Handle Function

Now we will play with “handle” function and change return 0; to echo “hi from my”.PHP_EOL; and resulted file will look like this

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class HiCommand extends Command
{

    protected $signature = 'say:hi';
    protected $description = 'This is my test command.';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        echo "hi from my own command".PHP_EOL;
    }
}

Run your command now

php artisan say:hi

output will be “hi from my own command”

Arguments in custom command

Now we want to add custom arguments in our command like php artisan say:hi Tahir, we can acheive this by changing signature argument will be in curly braces

protected $signature = 'say:hi {name}';

We can get this argument like this

$this->argument('name')

So our handle function will look like this now

public function handle()
{
   echo "hi ".$this->argument('name')." from my own command".PHP_EOL;
}

Run command “php artisan say:hi Tahir” and output will be

hi Tahir from my own command

Optional arguments in command

If we will run this command without argument it will throw and error so solution is just add a ? in arugment

protected $signature = 'say:hi {name?}';