# 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}
# Prepare
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
touch database/database.sqlite
# change database connection .env file
DB_CONNECTION=sqlite
# 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}
# 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
# Queue
Steps:
- Create queue table
- Migrate queue table
- Create a mail class, job class and controller class
- Run a worker with
queue:listen
thenqueue: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
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');
}
}
}
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";
}
}
- Setting on a production server
sudo apt-get install supervisor
cd /etc/supervisor/conf.d
sudo nano laravel-worker.conf
- 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
- Start Supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
- Restart queue if any change to your code
php artisan queue:restart
# 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
# Livewire
composer require livewire/livewire
php artisan make:livewire {ComponentName}
php artisan livewire:layout
php artisan serve
php artisan wire:navigate
Top comments (0)