0% found this document useful (0 votes)
8 views3 pages

Laravel Practical CheatSheet

Uploaded by

Steven Son
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Laravel Practical CheatSheet

Uploaded by

Steven Son
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Laravel Practical + Viva Cheat Sheet

1. Laravel Basics

- MVC Framework: Model (data), View (UI), Controller (logic)

- Artisan Commands:

- php artisan serve

- php artisan make:model ModelName -m

- php artisan make:controller ControllerName

- php artisan migrate

- Composer:

- composer create-project laravel/laravel project-name

2. Routing, Controllers & Views

- Basic Route:

Route::get('/hello', function () { return 'Hello World'; });

- Route with Parameter:

Route::get('/user/{id}', function ($id) {

return response()->json(['id' => $id, 'msg' => 'Welcome!']);

});

- Named Route & Redirection:

Route::get('/dashboard', [UserController::class, 'index'])->name('dashboard');

return redirect()->route('dashboard', ['user' => 5]);

3. Cookies

- Set Cookie:

return response('Set')->cookie('newsletter_preference', 'yes', 60*24*30);


- Delete & Update Cookie:

Cookie::queue(Cookie::forget('preferred_language'));

Cookie::queue('preferred_language', 'en', 60*24);

4. Sessions

- Set: session(['key' => 'value']);

- Get: session('key');

- Delete: session()->forget('key');

5. Validation

- Simple Validation:

$request->validate(['email' => 'required|email', 'password' => 'required|min:6']);

- Custom Messages:

$request->validate(['name' => 'required|alpha'], ['name.required' => 'Name is mandatory.']);

6. File Upload

if ($request->hasFile('file')) {

$file = $request->file('file');

$file->move(public_path('uploads'), $file->getClientOriginalName());

7. Blade

- Output: {{ $message }}

- Template Inheritance:

Layout: <html><body>@yield('content')</body></html>

Child: @extends('layouts.app') @section('content') Welcome! @endsection


8. Resource Controller

php artisan make:controller ProductController --resource

Route::resource('products', ProductController::class);

9. Middleware & Route Groups

Route::middleware(['auth'])->group(function () {

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

});

Route::prefix('admin')->group(function () {

Route::get('/dashboard', function () { return 'Admin Dashboard'; });

});

10. Database

- Migration:

Schema::create('students', function (Blueprint $table) {

$table->id(); $table->string('name'); $table->string('email');

$table->string('course'); $table->timestamps();

});

- CRUD (Eloquent):

Create: Student::create([...]);

Read: Student::all();

Update: $s = Student::find($id); $s->course = 'Laravel'; $s->save();

Delete: Student::destroy($id);

You might also like