Skip to content

Latest commit

 

History

History
362 lines (276 loc) · 10.7 KB

laravel-from-scratch.md

File metadata and controls

362 lines (276 loc) · 10.7 KB

Laravel from Scratch (Building Laravel Get Started Project)


Last Modified: Aug 11, 2024

Date: May 11, 2021

Note: For sample code follow repository mentioned below (at the end)

Steps

Step 1. Install Laravel

  • Laravel 11
    Note: If laravel installed globally (composer global require laravel/installer)
laravel new blog
  • Laravel 7 (older version)
composer create-project --prefer-dist laravel/laravel:^7.0 blog

Step 2. Add DB settings in .env file (No need. Follow CLI instructions)

  • Create DB and Add in .env file

Step 2.1 (Optional) Create virtual host (use valet. Also no need. Follow CLI instructions)

  • Create vhost for projectpath/public folder
  • Browse example.rob (virtual host)
  • You can also ignore vhost and simply run
php artisan serve

Step 3. Add permission to storage folder (May be no need)

  • Storage folder needs write permission
sudo chmod -R 0777 storage/

Note: For Linux and MacOS only

  • Now, you should be able to access project url (vhost or serve url)

Step 4. Laravel Auth Setup (May be no need. Follow CLI instructions)

We will use laravel default auth

  • Laravel 8
composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run dev
npm install && npm run dev
php artisan migrate
  • Laravel 7 (Older version)
composer require laravel/ui:^2.4
php artisan ui vue --auth
npm install && npm run dev
php artisan migrate

Step 5. Seed an admin user

  • Modify code in run() : database/seeders/DatabaseSeeder.php
User::factory()->create([
    'name' => 'Admin',
    'email' => 'admin@example.com',
]);
  • Run migration and seed
php artisan migrate:fresh --seed

Step 6. Install Nhrrob Crudgenerator

  • Install a crudgenerator that generate web and api files
composer require nhrrob/crudgenerator
php artisan vendor:publish

Step 7. (Optional) Install Laravel Passport for Rest Api

  • Nhrrob Crudgenerator generates basic crud as well as Rest crud files. If we want to use rest api then we need laravel passport. Its an optional step if we dont need rest api.

Link: https://github.com/nhrrob/laravelwiki/blob/master/laravel-passport-installation.md

  • If you run migrate and seed in the future, you need to re create personal access token. So, run
php artisan migrate:fresh --seed
php artisan passport:install

Step 8. (Optional) Clear cache and dump autoload (do it often)

  • Clear cache and dump autoload files.
sudo chmod -R 0777 storage/
sudo chmod -R 0777 bootstrap/cache
composer dump-autoload
php artisan optimize:clear
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:clear
clear

Step 9. Generate Web and Api Crud for Product

php artisan crud:generator
php artisan crud:generator --admin


Note: --admin creates crud under Admin folder

  • Add title field in migration files
$table->string('title');
  • Run migration
php artisan migrate
  • Browse siteurl/products
  • Test create, edit, delete and list
  • Cheers!

Note: If you want to create crud by your own, you may follow these repositories instead of creating by generator.

  1. Two rest api crud repositories:
    https://github.com/nhrrob/laravel-8-api-crud
    https://github.com/nhrrob/laravel-7-api-crud

  2. Two basic crud repositories:
    https://github.com/nhrrob/laravel-8-crud
    https://github.com/nhrrob/laravel-7-crud

  3. Rest Api Step by Step Guide: https://github.com/nhrrob/laravelwiki/blob/master/restful-api-crud-with-passport.md

Step 10. Test Api using Postman

Route::post('register', '\App\Http\Controllers\Api\AuthController@register');
Route::post('login', '\App\Http\Controllers\Api\AuthController@login');

Route::group(['middleware' => ['auth:api']], function () {
    Route::post('/logout', '\App\Http\Controllers\Api\AuthController@logout');
});
  • Testing: Follow below link

Link: https://github.com/nhrrob/laravelwiki/blob/master/restful-api-crud-with-passport.md
Step title: Test Using Postman (maybe: Step 8)

Step 11. Update product view files (Adding Layout)


Step 12. Lets Update Stub files for future cruds


Step 13. Lets generate another crud (to test stub changes)

  • As we changed stub files, now everything should work fine with project default layout.
php artisan crud:generator
php artisan crud:generator --admin
  • Model Title: Project
  • Add title field in migration file
$table->string('title');
  • Run migration and seed demo data
php artisan migrate:fresh --seed
php artisan passport:install
  • Backend: Lets browse siteurl/admin/projects; Also check api using postman.
  • Frontend: Lets browse siteurl/projects; Also check api using postman.

Step 14. Add NHR CSS Helper

  • Copy below gist code to public/css/nhrrob-css-helper.css file
    CSS Link: Gist: NHRRob CSS Helper

  • Add Helper CSS in your main layout (views/layouts/app.blade.php)

<link href="{{ asset('css/nhrrob-css-helper.css') }}" rel="stylesheet">
  • To test helper css: Example classes=> p-0, p-1, p-2, p-3, p-4
    (p-0 means padding: 0; 1 means 10px; 2= 20px)

Step 15. Spatie Laravel Permission Setup


Step 16. Add Dashboard page for all users

- routes/web.php
- app/Http/Controllers/Admin/DashboardController
- resources/views/admin/dashboard/index.blade.php

Step 17. Add style, script, admin layout

  • Createa two style files and two js files (backend and frontend)
- public/css/admin_style.css
- public/css/style.css

- public/js/admin_script.js
- public/js/script.js
  • Create resources/views/admin/layouts/app.blade.php add style in header and script in just before body tag.
    • Header (beofre head tag) : Example of frontend layout
    <link href="{{ asset('css/style.css') }}" rel="stylesheet">
    
    @yield('header')
    @yield('css')
    
    • Footer (before body tag) : Example of frontend layout
    <script src="{{{{ asset('js/script.js') }}}}"></script>
    @stack('js')
    
  • update your view files @extend (as we are using admin.layouts.app now)
  • Update app name in your .env to "Laravel Get Started"

Step 18: Add Role and Permission Crud

php artisan crud:generator --admin
  • Permission Crud

    • Remove generated Permission model and permissions migration
    • Update controller with spatie permission model class
    • Update Request and Resource (replace title with name)
    • Replace title with name field in view files. Add group_name field too.
    • Add pagination support (also add in stub files too): https://github.com/nhrrob/laravelwiki
  • Role Crud

    • Remove role migration and model files (we will use spatie)
    • Update Controller with spatie model
    • Replace title with name in controller and route (API); do the same for permission api too.
    • Replace title with name in Request and Resource and view files
    • Assigning permissions to roles :
      Check view and controller files of laravel get started project.
  • User Crud:

    • Take a backup of User model and create user crud using crud generator
    • remove migration file
    • update title to name for whole crud
    • sync Roles on store or update
      Sample code can be found here:
      Laravel Get Started Project => https://github.com/nhrrob/laravel-get-started-project

Step 19: Add Global Trait


We are done!

  • Congratulations! You have successfully built a Laravel Get Started Project.

Note

  • If you run php artisan migrate:fresh --seed then you will lose personal access token of passport.
  • So, in that case run again:
php artisan passport:install 

Bonus:

  • You can always run below commands
php artisan optimize:clear
sudo chmod -R 0777 storage/
sudo chmod -R 0777 bootstrap/cache
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
composer dump-autoload