DEV Community

Agus Sudarmanto
Agus Sudarmanto

Posted on • Edited on

Commands for Laravel

# Regular steps:

Create migration
Create model from migration
Setting environment file .env
Recompile assets
Start server

# Install Laravel

laravel new {folder-or-application-name}
- or -
composer create-project laravel/laravel {folder-or-application-name}
Enter fullscreen mode Exit fullscreen mode

# Prepare

composer install
copy .env.example .env
php artisan key:generate
php artisan migrate:refresh --seed
npm install # For frontend development
npm run dev
Enter fullscreen mode Exit fullscreen mode

# Using SQLite

touch database/database.sqlite

# change database connection .env file
DB_CONNECTION=sqlite
Enter fullscreen mode Exit fullscreen mode

# Using MySQL/MariaDB

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}
Enter fullscreen mode Exit fullscreen mode

# Artisan

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
Enter fullscreen mode Exit fullscreen mode

# 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

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
Enter fullscreen mode Exit fullscreen mode
Job
# 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');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
Controller
<?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";
  }
}
Enter fullscreen mode Exit fullscreen mode

- Setting on a production server

sudo apt-get install supervisor
cd /etc/supervisor/conf.d
sudo nano laravel-worker.conf
Enter fullscreen mode Exit fullscreen mode

- laravel-worker.conf file.

[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
Enter fullscreen mode Exit fullscreen mode

- Start Supervisor

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
Enter fullscreen mode Exit fullscreen mode

- Restart queue if any change to your code

php artisan queue:restart
Enter fullscreen mode Exit fullscreen mode

# Scheduling

# 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
Enter fullscreen mode Exit fullscreen mode

# Livewire

composer require livewire/livewire
php artisan make:livewire {ComponentName}
php artisan livewire:layout
php artisan serve
php artisan wire:navigate
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs