Skip to content

cleaniquecoders/training-2023-10-laravel-suk-pahang

Repository files navigation

CRUD

  • Controller
  • Route
  • View
  • Eloquent
    • Paginate
    • Create
    • Update
    • Delete
  • Policy
  • Seeder
  • Artisan
  • Package
    • Spatie - Laravel Permission
    • Laravel Pint - Laravel Pint is an opinionated PHP code style fixer for minimalists.
    • Larastan - Adds code analysis to Laravel improving developer productivity and code quality.

Controller

Create a single method controller:

php artisan make:controller WelcomeController --invokable

Create resource controller with binding model:

php artisan make:controller UserController --model=User

Route

Create route using closure:

Route::get('/', function() {
    return view('welcome');
});

Create route using view only:

Route::view('/', 'welcome');

Create route using single method / invokable controller:

use App\Http\Controllers\WelcomeController;

Route::get('/', WelcomeController::class);

Create resource controller routes:

use App\Http\Controllers\UserController;

Route::resource('users', UserController::class);

View

Looping:

@foreach($users as $user)
...
@endforeach

Control Structure:

@if(...)
...
@endif
@can('delete', $user)
...
@endcan

Eloquent

Get all data:

$users = User::all();
// OR
$users = User::get();

Paginate data:

$users = User::paginate();

Create new record:

$user = User::create([
    'name' => 'Nasrul',
    'email' => '[email protected]',
]);

Update record:

User::where('email', '[email protected]')
    ->update([
        'name' => 'Nasrul Hazim',
    ]);
// OR
$user->update([
    'name' => 'Nasrul Hazim',
]);

Delete record:

User::where('email', '[email protected]')->delete();
// OR
$user->delete();

Artisan

List of available commands:

php artisan

Create new seeder:

php artisan make:seeder PermissionSeeder

Create new controllers:

php artisan make:controller UserController --model=User

Create new policy:

php artisan make:policy UserPolicy --model=User

Seeder

Create new seeder:

php artisan make:seeder PermissionSeeder

Seed data:

php artisan db:seed

Seed specific seeder:

php artisan db:seed --class=PermissionSeeder

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages