Kotlin - Everything you need to know
Kotlin Details
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 --apiUsing a Controller
Import a
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 LogIpToDatabaseA 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 fileEdit 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 migrateAdd 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();