Last Modified: Aug 11, 2024
Date: May 11, 2021
Note: For sample code follow repository mentioned below (at the end)
- 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
- Create DB and Add in .env file
- Create vhost for projectpath/public folder
- Browse example.rob (virtual host)
- You can also ignore vhost and simply run
php artisan serve
- 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)
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
- 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
- Now, login with admin@example.com/password
- Install a crudgenerator that generate web and api files
composer require nhrrob/crudgenerator
php artisan vendor:publish
- 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
- 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
- Thanks to nhrrob/crudgenerator to make it happen!
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.
-
Two rest api crud repositories:
https://github.com/nhrrob/laravel-8-api-crud
https://github.com/nhrrob/laravel-7-api-crud -
Two basic crud repositories:
https://github.com/nhrrob/laravel-8-crud
https://github.com/nhrrob/laravel-7-crud -
Rest Api Step by Step Guide: https://github.com/nhrrob/laravelwiki/blob/master/restful-api-crud-with-passport.md
- Thanks to nhrrob/crudgenerator to make it happen!
- We need to add Api auth routes first as crudgenerator doesn't add it by itself but provide link to copy the code.
- Add below auth routes to routes/api.php (above your generated routes) Link: https://gist.github.com/nhrrob/fbc0857c3b5ed8c03ca8cc4ebdead749
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)
-
We will use laravel default design (provided layout)
-
Follow this repo to get the default layout (resources/views/product)
Laravel Get Started Project: https://github.com/nhrrob/laravel-get-started-project
-
Now, we will update crud generator related stub files. So, future cruds will have our project default layout
-
Sample code can be found here:
Laravel Get Started Project => https://github.com/nhrrob/laravel-get-started-project
- 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.
-
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)
- Help link: Spatie Laravel Permission Setup
-
Sample code can be found here:
Laravel Get Started Project => https://github.com/nhrrob/laravel-get-started-project -
Check below files
- routes/web.php
- app/Http/Controllers/Admin/DashboardController
- resources/views/admin/dashboard/index.blade.php
- 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"
- Sample code can be found here:
Laravel Get Started Project => https://github.com/nhrrob/laravel-get-started-project
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
- Add GlobalTrait and include it in Parent Controller (which is extended by all controllers)
- Gist link: https://github.com/nhrrob/laravelwiki
- Congratulations! You have successfully built a Laravel Get Started Project.
- 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
- 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