Commands for Laravel
Tags: php, laravel, cli, command
Published: 2023-08-05 12:29:39

## # Regular steps:
Create migration
Create model from migration
Setting environment file `.env`
Recompile assets
Start server

## # Install Laravel

```bash
laravel new {folder-or-application-name}
- or -
composer create-project laravel/laravel {folder-or-application-name}
```
## # Prepare
```sh{3,4}
composer install
copy .env.example .env
php artisan key:generate
php artisan migrate:refresh --seed
npm install # For frontend development
npm run dev
```

## # Using SQLite
```bash
touch database/database.sqlite

# change database connection .env file
DB_CONNECTION=sqlite
```

## # Using MySQL/MariaDB

```bash{1}
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={your-database-name}
DB_USERNAME={your-database-username}
DB_PASSWORD={your-database-password}
```

## # Artisan

```bash
php artisan make:model {Model-name} -m
php artisan make:migration {migration_name}
php artisan migrate
php artisan make:controller {controller-name}Controller
php artisan serve
```

## # Queue

#### Steps:

1. Create queue table
2. Migrate queue table
3. Create a mail class, job class and controller class
4. Run a worker with `queue:listen` then `queue:work`

#### - Prepare
```bash
php artisan queue:table
php artisan migrate

# set `QUEUE_CONNECTION=database` in .env file
php artisan make:mail PaymentMail
php artisan make:job Payment
php artisan make:controller PaymentController

php artisan queue:listen # executing the jobs
php artisan queue:work
```

##### Job
```php
# cli: php artisan make:job Payment
class Payment implements ShouldQueue
{
  public $timeout = 120;

  public function __construct(Message $email)
  {
  }

  public function handle()
  {
    Mail::send([], [], function ($message) use ($user,$html) {
      $message->to($user['email'])
              ->subject($this->subject)->setBody($html, 'text/html');
    }
  }
}
```

##### Controller
```php
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Jobs\SendEmailJob;

class EmailController extends Controller
{
  public function sendEmail()
  {
    dispatch(new SendEmailJob());
    echo "email sent";
  }
}
```


#### - Setting on a production server
```bash
sudo apt-get install supervisor
cd /etc/supervisor/conf.d
sudo nano laravel-worker.conf
```

#### - `laravel-worker.conf` file.
```conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your application/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=admin
numprocs=5
redirect_stderr=true
stdout_logfile=/path/to/your application/worker.log
```

#### - Start Supervisor
```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
```

#### - Restart queue if any change to your code
```bash
php artisan queue:restart
```

## # Scheduling

```bash
# create command and schedule
php artisan make:command GenerateInvoiceCommand
php artisan list
php artisan schedule:work

# use crontab
crontab –l # view all runing cron
crontab -e # accesss crontab file

# add this to crontab
* * * * * php /path-to-your-laravel-project/artisan schedule:run >> /dev/null 2>&1

# restart cron for our changes to take effect
sudo service cron restart
```

## # Livewire
```bash
composer require livewire/livewire
php artisan make:livewire {ComponentName}
php artisan livewire:layout
php artisan serve
php artisan wire:navigate
```