Laravel Cheatsheet to Learn Laravel Quickly

Follow @desiprogrammer_

Before anyone else says that a lot of things here are the exact same as of Laravel Official Docs, This blog/snippet/cheatsheet is just a simple straight forward extraction of a lot of things which are placed there.

Set Up

1. Install xampp
2. Add PHP to path
3. Download composer.prar
4. Add composer to path using option on website
5. Create a laravel project

Create a project

composer create-project laravel/laravel laravel_api_basics

Check out apis.php to add Routes and expirement with it.

Routing

Route::get('/profile', function(Request $request){
    return "Hello";
});

# Supported Route methods

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

To respont to multiple HTTP methods.

Route::match(['get', 'post'], '/', function () {
    //
});

Route::any('/', function () {
    //
});

Redirecting

Route::redirect('/here', '/there');

By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter:

Route::redirect('/here', '/there', 301);

Or, you may use the Route::permanentRedirect method to return a 301 status code:

Route::permanentRedirect('/here', '/there');

To prefix to a route, such as apis/v1/<any>

Route::prefix('admin')->group(function () {
    Route::get('/users', function () {
        // Matches The "/admin/users" URL
    });
});

Query Parameters

Adding a Controller

# --api adds methods for crud, not code only method
# remove --api if you want to create your own methods
php artisan make:controller <controllername> --api
# example : php artisan make:controller ProductController --api

Using a Controller

import the controller

use App\Http\Controllers\ProductController;

Map a route to a function in controller

Route::get('/shorten', [ProductController::class, 'index']);

Middlewares

Create a middleware

php artisan make:middleware LogIpToDatabase

A middleware is created in app/Http/Middleware

Follow this https://laravel.com/docs/8.x/middleware to register a middleaware globally or route specific.

To call a middleware from a Controller, Use a contructor : https://stackoverflow.com/questions/43804752/laravel-adding-middleware-inside-a-controller-function.

There areΒ 3 ways to use a middleware inside a controller.

1) Protect all functions in a Controller

 public function __construct()
 {
     $this->middleware('auth');
 }

2) Protect only some functions

 public function __construct()
 {
     $this->middleware('auth')->only(['functionName1', 'functionName2']);
 }

3) Protect all functions except some

 public function __construct()
 {
     $this->middleware('auth')->except(['functionName1', 'functionName2']);
 }

Here you can find all the documentation about this topic:Β Controller Middleware.

Models

Create a Model

php artisan make:model ProductModel --migration
// --migration to create a migration file

Edit table configuration in database>migrations>__created_migration.

Schema Docs : https://laravel.com/docs/4.2/schemahttps://laravel.com/docs/8.x/migrations#available-column-types

To Migrate

php artisan migrate

Add fillable in Model to specify what can be accept to store data in the database.

// example
protected $fillable = [
    'image',
    'title',
    'description',
];

Passing Body data to get values

$request->all();