Skip to content

Files

Latest commit

 

History

History
230 lines (162 loc) · 4.18 KB

INSTALL.md

File metadata and controls

230 lines (162 loc) · 4.18 KB

Laravel Logo

Laravel Project Docs

Setting up the project.

All commands executed on project directory windows cmd | powerShell | gitbash. Install Composer Link .

  1. Creating an Application :
laravel new <project-name>
  1. Go to project directory :
cd <project-name>
  • To open Vs code on project directotory
code .
  1. Install npm packages :
npm install && npm run build
  1. Run project server :
php artisan serve
  1. 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.

  1. 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=
  1. Make migrations :
php artisan migrate

Set up Login | Register | Email verificaton with Breeze

All commands executed on project directory command prompt | powerShell.

  1. Rerriev required packages to project :
 composer require laravel/breeze --dev
  1. Install required packages to project :
 php artisan breeze:install
  • Select blade as front end press enter twice
  1. 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"

Setting up API

Install nececessary packages :

php artisan install:api

Make Model

  1. Make model with neccesary documentation :
php artisan make:model <model-name> -a --api
  1. 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);
}
  1. 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();
    });
}
  1. Run migrations :
php artisan migrate

Setting up API routes

Edit files to create API routes

  1. Set-up API resource in routes\api.php :
use App\Http\Controllers\<Controller-class>;

Route::apiResource('posts', <Controller-class>::class);
  1. List routes on powershell :
php artisan route:list
  1. Select data :

Set controller function to get SELECT * FROM <table-name> in laravel

public function index()
{
    $products = product::all();
    return $products
}
  1. 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'])];
}