All commands executed on project directory windows cmd | powerShell | gitbash. Install Composer Link .
- Creating an Application :
laravel new <project-name>
- Go to project directory :
cd <project-name>
- To open Vs code on project directotory
code .
- Install npm packages :
npm install && npm run build
- Run project server :
php artisan serve
- Start Frontend server :
composer run dev
All migrations are done automatically if using SQLite, if not have to make migrations manually for eaxmple MySQL on xampp.
- Create DB connection on
.env
:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=example-app
DB_USERNAME=root
DB_PASSWORD=
- Make migrations :
php artisan migrate
All commands executed on project directory command prompt | powerShell.
- Rerriev required packages to project :
composer require laravel/breeze --dev
- Install required packages to project :
php artisan breeze:install
- Select blade as front end press enter twice
- Email Varification :
Change Auth user model in app\Models\User.php
.
- uncomment this line
use Illuminate\Contracts\Auth\MustVerifyEmail;
- add
MustVerifyEmail
to user class
class User extends Authenticatable implements MustVerifyEmail
Configure .env
file with email and App password with gmail.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME= <example@gmail.com>
MAIL_PASSWORD= <email app password>
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS= <same email>
MAIL_FROM_NAME="Laravel App"
Install nececessary packages :
php artisan install:api
- Make model with neccesary documentation :
php artisan make:model <model-name> -a --api
- Setup created model in
app\Models\<model-name>.php
:
class <model-name> extends Model
{
use HasFactory;
protected $fillable = ['Column1', 'Column2'];
}
- If forieng key is used, in this esample Auth:user model is forieng key. Import required models before ading.
public function user()
{
return $this->belongsTo(User::class);
}
- Editing user model
public function products()
{
return $this->hasMany(products::class);
}
- Setup migration :
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string("name")->unique();
$table->decimal("price", 10, 2);
$table->integer("quantity");
$table->text("description");
$table->foreignId('user_id')->constrained()->cascadeOnDelete(); // if forieng key is added
$table->timestamps();
});
}
- Run migrations :
php artisan migrate
Edit files to create API routes
- Set-up API resource in
routes\api.php
:
use App\Http\Controllers\<Controller-class>;
Route::apiResource('posts', <Controller-class>::class);
- List routes on powershell :
php artisan route:list
- Select data :
Set controller function to get SELECT * FROM <table-name>
in laravel
public function index()
{
$products = product::all();
return $products
}
- Set sanctum authentication to controller :
- Import this first
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
- change controller first line
class <Controller-class> extends Controller implements HasMiddleware
- Set middlewear with exeptions(functions)
public static function middleware()
{
return [new Middleware('auth:sanctum', except: ['exeption1', 'exeption2'])];
}